Node.js Async Module–Waterfall and Series

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.

  1. Waterfall
  2. 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');

image

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');

image

 

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');

image

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');

image

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.

In next tutorial, we are going to talk more about async.auto() and async.parallel(). Stay tuned. Happy Coding!!!

You may also like...

  • Bipin Bhandari

    Thank you very much!

  • Imran Shaikh

    very fantastic explanation i was struggling to learn async.series and waterfall
    but u have explained a well so thank u very much and keep updating us….

  • deiva deiva

    Very clear explanation but i got the output for waterfall with error is as follows
    Program Start
    First Step –>
    Main Callback –> 1
    Program End

  • ramiramilu

    @deivadeiva:disqus I think you are executing the second code snippet where I am explicitly calling error callback. Please check, I am able to get right response when I execute first code snippet.