0.0.3 • Published 4 years ago
babel-plugin-async-function-try-catch v0.0.3
babel-plugin-async-function-try-catch
Add try catch code blocks to async functions
Example
In
async function foo(){
await bar();
}
Out
async function foo() {
try {
await bar();
} catch (error) {}
}
Out with options
Handle uncaught errors with handleError
import { handleError } from "/src/utils/handleError";
async function foo() {
try {
await bar();
} catch (error) {
handleError(error);
}
}
Install
npm:
npm install --save-dev babel-plugin-async-function-try-catch
yarn:
yarn add -D babel-plugin-async-function-try-catch
Usage
With a configuration file
Without options:
{
"plugins": [
"babel-plugin-async-function-try-catch"
]
}
With options:
{
"plugins": [
["babel-plugin-async-function-try-catch", {
"injectImport": {
"specifier": "handleError",
"source": "/src/utils/handleError.js"
}
}]
]
}
With a CLI
babel --plugins babel-plugin-async-function-try-catch script.js
With a Node API
require("@babel/core").transform("code", {
plugins: ["babel-plugin-async-function-try-catch"]
});
Options
interface Options {
// inject handler function
injectImport: {
// identifier of the handler function
specifier: string;
// source path of the handler function
source: string;
// compare sourcePath and tragetPath, if true,
// the handler function will be inject by sourcePath
checkSourceEqual(sourcePath, targetPath): boolean;
}
}