Async Module–Waterfall and Series in Node.js

By | November 6, 2017

Async module in Node.js provide a way to structure and organize our JavaScript Asynchronous callbacks code. In traditional coding practice, we nest multiple callbacks in the same function, that creates lot of confusion in code readability and maintainability. Async Module addresses the afore said problem by providing powerful functions through which we can get different combinations of Async and Sync operations.

In this tutorial I am going to show two major control flow techniques in Async Module.

  • Waterfall
  • Series

Waterfall:

In Waterfall, we pass array of functions to async.waterfall(). All the functions are executed in series and output of each function is passed to the next function. In case if any error is passed to the function’s callback, then main callback function will be invoked by skipping rest of functions in the queue.

console.log('Program Start');

var async = require('async');
async.waterfall([
    function (callback) {
        console.log('First Step --> ');
        callback(null, '1', '2');
    },
    function (arg1, arg2, callback) {
        console.log('Second Step --> ' + arg1 + ' ' + arg2);
        callback(null, '3');
    },
    function (arg1, callback) {
        console.log('Third Step --> ' + arg1);
        callback(null, 'final result');
    }
], function (err, result) {
    console.log('Main Callback --> ' + result);
});

console.log('Program End');

 

In case if we pass error object to any function callback as shown below, we get main callback executed.

 

console.log('Program Start');

var async = require('async');
async.waterfall([
    function (callback) {
        console.log('First Step --> ');
        callback('error', '1', '2');
    },
    function (arg1, arg2, callback) {
        console.log('Second Step --> ' + arg1 + ' ' + arg2);
        callback(null, '3');
    },
    function (arg1, callback) {
        console.log('Third Step --> ' + arg1);
        callback(null, 'final result');
    }
], function (err, result) {
    console.log('Main Callback --> ' + result);
});

console.log('Program End');

 

Series:

In Series, we pass array of functions to async.series(). All the functions are executed in series and the consolidated outputs of each function is passed to the final callback. In case if any error is passed to the function’s callback, then main callback function will be invoked by skipping rest of functions in the queue.

console.log('Program Start');

var async = require('async');
async.series([
    function (callback) {
        console.log('First Step --> ');
        callback(null, '1');
    },
    function (callback) {
        console.log('Second Step --> ');
        callback(null, '2');
    }
],
function (err, result) {
    console.log(result);
});

console.log('Program End');

In case if we pass error object to any function callback as shown below, we get main callback executed.

console.log('Program Start');

var async = require('async');
async.series([
    function (callback) {
        console.log('First Step --> ');
        callback('error', '1');
    },
    function (callback) {
        console.log('Second Step --> ');
        callback(null, '2');
    }
],
function (err, result) {
    console.log(result);
});

console.log('Program End');

One critical difference between Series and Waterfall can be understood by carefully looking at “Program Start” and “Program End” status. In Waterfall, Program End message is printed before executing async functions. But in series, Program End message is printed only after executing async functions.

 

I hope you will enjoy the Async Module–Waterfall and Series in Node.js. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Like it? Share it