Quantcast
Channel: Rami Vemula » NodeJs
Viewing all articles
Browse latest Browse all 4

Node.js Async Module–Auto and Parallel

$
0
0

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 previous tutorial we looked at Waterfall and Series control flows, in this tutorial we are going to see following control flows.

  1. Auto
  2. Parallel

Auto

In Auto, we pass array of function to async.auto(). In this control flow. we can make functions dependent on other functions. We can also have functions which are independent of other functions. If there is a dependency for a function, then that particular function will only execute once the dependent function is executed. Functions which don’t have any dependencies will run independently.

console.log('Program Start');

var async = require('async');
async.auto({
    one: function (callback, arg1) {
        setTimeout(function () {
            console.log('First Step --> ' + JSON.stringify(arg1));
            callback(null, '1');
        }, 3000);
    },
    two: [
        'one', function (callback, arg1) {
            console.log('Second Step --> ' + JSON.stringify(arg1));
            callback(null, '2');
        }
    ],
    three: [
        function (callback) {
            console.log('thrid Step');
            callback(null, '3');
        }
    ]
},
    function (err, result) {
    console.log(result);
});

console.log('Program End');

In above code sample, function TWO is dependent on ONE. But function THREE is independent of other two functions. As we set a timeout for function ONE for 3secs, function THREE will execute first. Function TWO cannot execute because it is dependent on function ONE. So after 3secs of wait time, when function ONE executes successfully, then function TWO executes.

image

Interesting fact about Auto control flow is with its results delegation. Results of each function which was completed successfully will be passed to next function in execution pipeline. At the end, all of the cumulative results will be passed to the final callback. In case of any error, we can pass the same as error parameter to the next callback, auto will not execute next callback. It will simply ignore all the other functions in the pipeline and execute the final callback as shown below.

console.log('Program Start');

var async = require('async');
async.auto({
    one: function (callback, arg1) {
        setTimeout(function () {
            console.log('First Step --> ' + JSON.stringify(arg1));
            callback('error', '1');
        }, 3000);
    },
    two: [
        'one', function (callback, arg1) {
            console.log('Second Step --> ' + JSON.stringify(arg1));
            callback(null, '2');
        }
    ],
    three: [
        function (callback) {
            console.log('thrid Step');
            callback(null, '3');
        }
    ]
},
    function (err, result) {
    console.log(result);
});

console.log('Program End');

image

As we introduced error in function ONE, function TWO was never executed and the final callback was executed.

Parallel

In Parallel, we pass array of function to async.parallel(). In this control flow. all the functions are executed in parallel, without being dependent on other function. All the results of functions are cumulatively passed to final callback.

console.log('Program Start');

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

console.log('Program End');

Even though first and third function got a delay attached for their execution, the results which are passed to final callback are ordered in the sequence of steps (as shown below).

image

In case of any function passing error to the callback, final callback will be executed with the results which are available till the error occurrence. once final callback executed, remaining functions (which are not executed) will be executed.

console.log('Program Start');

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

console.log('Program End');

image

In above code sample, as we got error in third step, final callback is executed after third step. After final callback, first step is executed (because of the delay attached to its execution).

With this we conclude this tutorial. In next set of tutorials, we will see how to create our own modules in Node.js. Stay tuned. Happy Coding!!!


Viewing all articles
Browse latest Browse all 4

Trending Articles