babel-plugin-async-function-try-catch

0.0.3 • Public • Published

babel-plugin-async-function-try-catch

Add try catch code blocks to async functions

npm license badge

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;
  }
}

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i babel-plugin-async-function-try-catch

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

7.63 kB

Total Files

6

Last publish

Collaborators

  • zhijiang3