2.0.5 • Published 5 years ago

async-catch-loader v2.0.5

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

async-catch-loader

一个自动给 async 函数注入 try/catch 的 webpack loader

在开发中经常会使用 async/await 异步编程,同时也会频繁的使用 try/catch 捕获异步中的错误,使得业务代码充斥这 try/catch 非常的冗余,使用这个 loader 可以只在打包后的代码自动注入 try/catch,使得业务代码非常简洁

async function func() {
    let res = await new Promise(resolve => {
        setTimeout(() => {
            resolve('success')
        }, 3000)
    })
}

打包后自动注入 try/catch

async function func() {
    try {
       let res = await new Promise(resolve => {
            setTimeout(() => {
                resolve('success');
            }, 3000);
        });
    } catch (e) {
    //...
    }
}

2.0

只捕获最外层的 async 函数,防止之前版本捕获每个 await 导致变量访问不到程序崩溃的问题

Install

npm i async-catch-loader -D

Usage

// webpack.config.js

module: {
    rules: [
        {
            test: /\.js$/,
            use:{
                loader:'async-catch-loader',
                options:{
                    catchCode:`alert(e)`
                }
            }
        }
    ]
}

Options

NameTypeDefaultDescription
identifier{String}"e"catch 子句中的错误对象标识符
catchCode{String}console.error(e)catch 子句中的代码片段
finallyCode{String}undefinedfinally 子句中的代码片段
2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago