I came across this scenario while coding recently. I have an async function like this.
exports.dummy = function (req, res, next) {
async.waterfall([
function (cb) {
/.../
cb();
}, function (cb1) {
if (/*something*/) {
cb1();
} else {
next(); //my question is about this one
}
}, function (cb2) {
/*...*/
cb2();
}
], next);
};
This was my sample scenario where I called parent callback "next" in middle of async, and got this doubt. Does async wait for its internal callback "cb1" or terminates it? If it terminates, what about performance? Anything to take care about these callbacks?
Sandeep Panda
co-founder, Hashnode
Ok. So, here is how it works.
async.waterfallexpects each task to call itscb()in order to pass the control to the next callback. But if one of the tasks in the array passes an error to the callback i.e.cb(err);then the last callback executes immediately, which isnextin your case. So, instead of callingnext()from your second task you should just callcb1(err)which will invoke the final callback i.e.next.