zoukankan      html  css  js  c++  java
  • async Make errors in callbacks throw globally

    https://github.com/caolan/async/pull/1408

    let see an example:

    async.waterfall([
        function (cb) {
            const firstName = 'leeho';
            const email = '11';
            model.User.create({firstName, email})
                .then(instance => {
                    console.log('one')
                    cb(null, instance);
                }).catch(cb);
        },
        function (instance, cb) {
            // try {
            //     model.User.update({firstName: instance.firstName, lastName: balabala}, {where: {firstName: firstName}})
            //         .catch(cb)
            // } catch (e) {
            //     cb(e)
            // }
            model.User.update({firstName: instance.firstName, lastName: balabala}, {where: {firstName: firstName}})   //balabala is not defined
                .catch(cb)
            
        }
    ], function (err, result) {
        if (err) console.log('result err', err);
        console.log(result);
    })

    balabala is not defined .. and the async will throw error globally.

    async waterfall source code:

    export default  function(tasks, callback) {
        callback = once(callback || noop);
        if (!Array.isArray(tasks)) return callback(new Error('First argument to waterfall must be an array of functions'));
        if (!tasks.length) return callback();
        var taskIndex = 0;
    
        function nextTask(args) {
            var task = wrapAsync(tasks[taskIndex++]);
            task(...args, onlyOnce(next));
        }
    
        function next(err, ...args) {
            if (err === false) return
            if (err || taskIndex === tasks.length) {
                return callback(err, ...args);
            }
            nextTask(args);
        }
    
        nextTask([]);
    }

    the  wrapAsync function will wrap a function and return promise.the callback will be executed as part of the promise’s .then() method.This means that any error thrown from that method will be silenced, and lead to a “unhandled rejection”.the only way to handle it is use try catch?

  • 相关阅读:
    ASP.NET---创建自定义Web控件小结
    ASP.NET---使用Asp.NET开发三层架构的应用程序
    poj 1847 最短路简单题,dijkstra
    HDU 1102 最小生成树裸题,kruskal,prim
    poj 2239 二分图最大匹配,基础题(待补)
    HDU 1520 树形dp裸题
    HDU 2089 简单数位dp
    poj 3254 状压dp入门题
    HDU 1710 二叉树遍历,输入前、中序求后序
    Poj 3250 单调栈
  • 原文地址:https://www.cnblogs.com/ybleeho/p/9707916.html
Copyright © 2011-2022 走看看