Giter Club home page Giter Club logo

babel-plugin-wrap-trycatch's Introduction

一个自动添加用try catch包裹await的插件

开发调试

0、npm install

1、yarn test

提供给其他人用

npm publish发布,会预先执行npm run prepare

示例:

1、声明函数

编译前

async function fetchGet() {
  const res = await fetch('https://httpbin.org/get').then(res => res.json());
  return res;
}

编译后

async function fetchGet() {
  try {
    const res = await fetch('https://httpbin.org/get').then(res => res.json());
    return res;
  } catch (e) {
    console.log('error', e);
  }
}

2、箭头函数

编译前

const arrowFun = async () => {
  const res = await fetch('https://httpbin.org/post').then(res => res.json());
  return res;
}

编译后

const arrowFun = async () => {
  try {
    const res = await fetch('https://httpbin.org/post').then(res => res.json());
    return res;
  } catch (e) {
    console.log('error', e);
  }
};

3、没有包裹体的箭头函数

编译前

const arrowFunWithoutBody = async () => await fetch('https://httpbin.org/get/arrow');

编译后

const arrowFunWithoutBody = async () => {
  try {
    return await fetch('https://httpbin.org/get/arrow');
  } catch (e) {
    console.log('error', e);
  }
};

4、用if包裹await

编译前

const wrapWithIfFun = async () => {
  if ('True') {
    const res = await fetch('https://httpbin.org/get/arrow');
    return res;
  }
};

编译后

const wrapWithIfFun = async () => {
  try {
    if ('True') {
      const res = await fetch('https://httpbin.org/get/arrow');
      return res;
    }
  } catch (e) {
    console.log('error', e);
  }
};

5、已经被tryCatch包裹

编译前

const awaitWrapByTryCatchFun = async () => {
  try {
    const res = await fetch('https://httpbin.org/post').then(res => res.json());
    return res;
  } catch (err) {
    console.log('自定义捕捉错误');
  }
}

编译后

const awaitWrapByTryCatchFun = async () => {
  try {
    const res = await fetch('https://httpbin.org/post').then(res => res.json());
    return res;
  } catch (err) {
    console.log('自定义捕捉错误');
  }
};

babel-plugin-wrap-trycatch's People

Stargazers

马正贤 avatar

Watchers

马正贤 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.