简体   繁体   中英

Where is the async.waterfall callback defined

I am using the async library and am using the async.waterfall function whose signature is async.waterfall([function1,function2,function3],function(err,result){}) . Now i understand that the results of each function is passed on to the next and the final callback function is executed in case of some error in between or when the execution is over.

What i do not understand is that each function in the array of functions passed takes a callback as an argument and the callback is executed in each function with the result of that function. But i do not understand where that callback function is defined.

Below is a sample piece of code

 async.waterfall([
 function getUserAvatar(callback) {
    console.log("Callback function is " + callback)//print the 
     callback definition function 
    github.search.users({ q: 'airbnb' }, function(err, res) {
        if (err) {
            callback(err, null);
            return;
        }

        var avatarUrl = res.items[0].avatar_url;
        callback(null, avatarUrl);
    });
  },
  function wrapAvatarInHtml(avatarUrl, callback) {
    var avatarWithHtml = '<img src="' + avatarUrl + '"/>';
    callback(null, avatarWithHtml);
}
], function(err, result) {
 if (err) {
    console.error(err);
    return;
 }
console.log(result);
})

When i execute the above code, the line where i do console.log("Callback function is " + callback) it prints the below definition.

function () {
        if (fn === null) throw new Error("Callback was already called.");
        var callFn = fn;
        fn = null;
        callFn.apply(this, arguments);
    }

Now i do not understand where it is getting this definition. I searched the async library also for this but i could not find it. Can anyone explain this.

If you go through the code in this link . The next() defined is the callback being called. It is implicit, you don't have to define and is because even the tasks you are passing to async waterfall is not being called by you, but by async.waterfall()

Here is the library code of async waterfall. You will get the fair idea about how it works

exports.default = function (tasks, callback) {
    callback = (0, _once2.default)(callback || _noop2.default);
    if (!(0, _isArray2.default)(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 = (0, _wrapAsync2.default)(tasks[taskIndex++]);
        args.push((0, _onlyOnce2.default)(next));
        task.apply(null, args);
    }

    function next(err /*, ...args*/) {
        if (err || taskIndex === tasks.length) {
            return callback.apply(null, arguments);
        }
        nextTask((0, _slice2.default)(arguments, 1));
    }

    nextTask([]);
};

For more information, please install the async library and look into the node modules folder. You will get the complete idea of async library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM