In node.js module async there is a method called waterfall https://github.com/caolan/async#waterfalltasks-callback
I am perplexed as to why there is an optional callback with the description
Runs the tasks array of functions in series
Adding a callback seems pointless if the functions are executed in order then the last function in the sequence is the last. Isn't that like reading a story then saying "The End, The End"
Also it seams like litter to pass the callback down through the entire chain of functions.
Is there any practical point to it??
Yes; if one of the array functions fails and calls back with a non-null err
, then the rest of the array is skipped and the flow of execution short-circuits to that second function with the error. Otherwise, the second function is called with the result.
Sure, if there is guaranteed no error, and the array in its entirety does execute, then theoretically the second function could be included in the original array with no side-effects. However, that breaks the commonality of the async paradigm within Node.JS.
For instance:
async.waterfall([
function(callback) { // Runs, of course
callback();
}, function(callback) { // Also runs
callback('error!');
}, function(callback) { // Never runs
callback();
}], function(err) {
console.log(err);
});