• 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?

  • 相关阅读:
    你欠我的幸福,怎么弥补
    爱,请你走开
    一生为你
    爱你到底
    粒子滤波简介(转载)
    关于小波变换和Gabor变换的一些知识!
    基于Opencv的MeanShift跟踪算法实现
    opencv学习网页
    基于OpenCV库的Gabor滤波器的实现
    Mean Shift算法(CamShift)
  • 原文地址:https://www.cnblogs.com/ybleeho/p/9707916.html
Copyright © 2020-2023  润新知