1.0.5 • Published 4 years ago

await-catch-loader v1.0.5

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

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)
1.0.5

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago