🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

await-catch-loader

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

await-catch-loader

统一处理await形式promise,捕获错误

latest
Source
npmnpm
Version
1.0.5
Version published
Maintainers
1
Created
Source

await-catch-loader

统一处理await形式promise,捕获错误

安装

npm install await-catch-loader -D

作用

太多的try catch注入业务代码,不易于维护


// 使用loader前

async function a() {
  console.log('分割线')
  await new Promise((resovle, reject) => {
    reject('抛出错误')
  })
  console.log('分割线')
}

// 使用loader后

async function a() {
  console.log('分割线')
  try {
    await new Promise((resovle, reject) => {
      reject('抛出错误')
    })
  } catch(e) {
    console.error(e)
  }
  console.log('分割线')
}

使用


module: {
  rules: [{
    test: /\.js$/,
    use: {
      loader: 'await-catch-loader',
      options: {
        catch:`console.error(e)`,
        catchArg: 'e'
      }
    }
  }]
},

options

nametypedefaultdescription
catchstring|functionconsole.error(e)catch子句中的代码片段
catchArgstringecatch子句中的错误对象标识符(如果catch参数类型为函数,并且拥有参数,优先级大于catchArg)

Keywords

js

FAQs

Package last updated on 19 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts