GithubHelp home page GithubHelp logo

my-plugin / async-catch-loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yeyan1996/async-catch-loader

0.0 1.0 0.0 87 KB

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

License: MIT License

JavaScript 100.00%

async-catch-loader's Introduction

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

Name Type Default Description
identifier {String} "e" catch 子句中的错误对象标识符
catchCode {String} console.error(e) catch 子句中的代码片段
finallyCode {String} undefined finally 子句中的代码片段

async-catch-loader's People

Contributors

yeyan1996 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.