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?

 

 

posted @ 2018-09-26 16:48  ybleeho  阅读(171)  评论(0编辑  收藏  举报