English | 中文
A webpack loader that can automatically injects try/catch into async function
In development, async/await asynchronous programming is often used, and try/catch is frequently used to catch asynchronous errors, which makes the business code full of try/catch very redundant.
Using this loader, you can only automatically inject try/catch into the packaged code. Makes the business code very simple
async function func() {
let res = await new Promise(resolve => {
setTimeout(() => {
resolve('success')
}, 3000)
})
}
try/catch is automatically injected after packaging
async function func() {
try {
let res = await new Promise(resolve => {
setTimeout(() => {
resolve('success');
}, 3000);
});
} catch (e) {
//...
}
}
npm i async-catch-loader -D
// webpack.config.js
module: {
rules: [
{
test: /\.js$/,
use:{
loader:'async-catch-loader',
options:{
catchCode:`alert(e)`
}
}
}
]
}
Name | Type | Default | Description |
---|---|---|---|
identifier |
{string} |
"e" |
The error object identifier in the catch clause |
catchCode |
{string} |
"console.error(e)" |
Code snippet in the catch clause |
finallyCode |
{string} |
undefined |
The code snippet in the finally clause |