Node.js – Using the Async Module

the node Async Module

The Async Module

The Async module has a technique called series () that allows us to control the flow of our callbacks. The series () method takes an array of functions as an argument (or an object containing functions). Functions is executed in the order in which they exist in the array. Each callback function in the list takes an err parameter as the first argument to the function and the second.

The argument is the return value, if any. Soap opera the function also takes a second argument, which is a callback function contains the results of the functions passed as the first parameter. To demonstrate how the series () method can clean up our code by following the example has three timer tasks, each task is executed as a callback inside the other to the very top or to the first timer task. The code looks like this:

var results = [];
setTimeout(function() {
console.log("Task 1");
results[0] = 1;
setTimeout(function() {
console.log("Task 2"); results[1] = 2;
setTimeout(function() {
console.log("Task 3"); results[2] = 3;
}, 100);
}, 200);
}, 300);

As you can see, it looks a bit complicated and messy. The example below shows the same code rewritten using the async.series() function:

var async = require("async");
async.series([
function(callback) {
setTimeout(function() {
console.log("Task 1");
callback(null, 1);
}, 300);
},
function(callback) {
setTimeout(function() {
console.log("Task 2");
callback(null, 2);
}, 200);
},
function(callback) {
setTimeout(function() {
console.log("Task 3");
callback(null, 3);
}, 100);
}
], function(error, results) {
console.log(results);
});

Nice improvement. Now the code is executed sequentially, but is actually executed asynchronously (in each callback) using async.series () function. The sequential function is good if you need execute a set of asynchronous functions in a specific order.

You can also use the async.parallel () method for parallel execution callback. Difference between series and the parallel is that the series waits for one function to complete before executing
next, but no parallels. Using the above example, but changing it to parallel:

var async = require("async");
async.series([
function(callback) {
setTimeout(function() {
console.log("Task 1");
callback(null, 1);
}, 300);
             },
function(callback) {
setTimeout(function() {
console.log("Task 2");
callback(null, 2);
}, 200);
},
function(callback) {
setTimeout(function() {
console.log("Task 3");
callback(null, 3);
}, 100);
        }
], function(error, results) {
console.log(results);
                      });

Executing the above code should complete first task 3 and then task 2. and finally task 1. Using concurrency is the expected behavior of your code. when using asynchronous callbacks while the series provides callbacks in order, regardless of timers, etc. What if you need to complete part of your callbacks using sequences sequentially, restricting these other functions works in parallel? The answer to this question is the parallel.Limit () method. This the method takes an additional argument to indicate the positional limit of the latter a function to be executed in parallel:

var async = require("async");
async.parallelLimit([
function(callback) {
setTimeout(function() {
console.log("Task 1");
callback(null, 1);
}, 300);
},
function(callback) {
setTimeout(function() {
console.log("Task 2");
callback(null, 2);
}, 200);
},
function(callback) {
setTimeout(function() {
console.log("Task 3");
callback(null, 3);
}, 100);
}
], 2, function(error, results) {
console.log(results);
       });

After running this code, you should see that task 2 finishes first and then task 1 and finally task 3 which was not included in the parallel task as this was the third position in our array of functions.

There is also an async.waterfall () method that allows you to pass values between seep down style. The waterfall method is useful when you need the results of a previous function to perform an operation with next article in the series. An example is shown below:

var async = require("async");
async.waterfall([
function(callback) {
callback(null, 12, 15);
},
function(a, b, callback) {
callback(null, (a + b) * 10);
},
function(cc, callback) {
callback(null, Math.sqrt(cc));
}
], function(error, c) {
console.log(c);
});

In the above example, I add the numbers 12 and 15 which are processed second function. The result of this addition is passed to the next function as the cc parameter applied to the sqrt function. The end result is a square the root of (12 + 15) * 10. The final result is passed using the c parameter to the last callback function of the waterfall method. Notice how the waterfall method allows you to pass additional parameters to your callback functions, not just the err error and the return value, as is the case with the series method.