What is the difference between async.waterfall and async.series? - GeeksforGeeks

24 Oct, 2021
async.waterfall: This waterfall method runs all the functions(i.e. tasks) one by one and passes the result of the first function to the second, second function’s result to the third, and so on. When one function passes the error to its own callback, then the next functions are not executed. So, the main callback is immediately called with the error. 
Syntax:
waterfall(tasks,callback)
tasks: It refer to the array of async functions. Each function runs and sends the result as an argument to the next function.
callback:  Once all the functions are completed, then callback  runs. This will have arguments from the result of the last task’s callback. The callback is optional. It is invoked with (err,[results]).
Note: To use the async.waterfall function, we need to import it first.
javascript
import waterfall from 'async/waterfall';
Example: 
javascript
async.waterfall([    function(callback) {        callback(null, 'gfg', 'one');    },    function(arg1, arg2, callback) {        // The arg1 now equals 'gfg'        // and arg2 now equals 'one'        callback(null, 'two');    },    function(arg1, callback) {        // The arg1 now equals 'two'        callback(null, 'complete');    }], function (err, result) {    // Result now equals 'complete'});
Output: The async.waterfall() will pass only the result of the last function called to the main callback.
complete
async.series: The async.series method runs the functions in the tasks collection in series. Each one runs once the previous function has completed. If any functions in the series pass an error to its callback, no more functions are executed. Then, the callback is immediately called with the value of the error. Otherwise, callback receives an array of results of all the tasks when tasks have completed.  Syntax:
series(tasks,callback)
tasks: It is a collection of tasks or functions in series.
callback: It is an optional argument, invoked when error occurs in any of the function, otherwise, it gets the array of the results of all the tasks.
Note: To use the async.series function, we need to import it first.  
javascript
import series from 'async/series';
Example: 
javascript
async.series([    function(callback) {        // do something...        callback(null, 'one');    },    function(callback) {        // do something...        callback(null, 'two');    }],// callback(optional)function(err, results) {    /* Results is now equal to       ['one', 'two'] and this is       returned from all the tasks       executed above.*/});
Output: The  async.series(), once the series have finished, will pass all the results from all the tasks to the main callback.
The main difference between async.waterfall and async.series is that: 
The async.waterfall allows each function to pass on its results to the next function, while async.series passes all the task’s results to the final callback.
The async.waterfall() will pass only the result of the last function called to the main callback. But, the async.series will pass all the task’s result to the main callback.
saurabh1990aror
JavaScript-Misc
Picked
Difference Between
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Prim's and Kruskal's algorithm for MST
Difference between Internal and External fragmentation
Difference between Compile-time and Run-time Polymorphism in Java
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?