Blog Post

No logo
Node.js Async Module–Waterfall and Series

Async

    Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript.
    Although originally designed for use with Node.js and installable via npm install --save async, it can also be used directly in the browser.

Async-waterfall

    Its very simple,isolated async waterfall module for javacript. Runs an array of functions in series, each passing their results to the next in the array.  
    However, if any of the functions pass an error to the callback, the next function is not executed and the main callback is immediately called with the error.

    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.

Installation

    Just include async-waterfall before your scripts.
    npm install async-waterfall if you’re using node.js.
    component install es128/async-waterfall if you’re using component(1).
    bower install async-waterfall if you’re using Twitter Bower.

Usage

    waterfall(tasks, optionalCallback);
    tasks - An array of functions to run, each function is passed a callback(err, result1, result2, ...) it must call on completion.
    The first argument is an error (which can be null) and any further arguments will be passed as arguments in order to the next task.
    optionalCallback - An optional callback to run once all the functions have completed. This will be passed the results of the last task's callback.

Node.js:

   var waterfall = require('async-waterfall');
   waterfall(tasks, callback);
    
    Browser:
    
    // component(1)  
    var waterfall = require('async-waterfall');
    waterfall(tasks, callback);
 
    // Default:  
    window.asyncWaterfall(tasks, callback);

Async-series

    Run a series of callbacks in sequence, as simply as possible.
    async.series passes all results to the final callback
    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.

   Installation

    npm install async-series

   Usage

     series(tasks, callback, safe)
     Where tasks is an array of functions, each with their own done argument.  
     callback is called when finished. Setting safe to true will ensure there's at least a tick between each task to prevent RangeErrors.

    series([
    function(done) {
    console.log('first thing')
    done()
    },
    function(done) {
    console.log('second thing')
    done(new Error('another thing'))
    },
    function(done) {
    // never happens, because "second thing"  
    // passed an error to the done() callback  
    }
    ], function(err) {
    console.log(err.message) // "another thing"  
    })

Main difference between Waterfall and series
 
    Program end message in waterfall is printed before executing async function
    Program end message in series is printed after executing async function.