Iterating collections asynchronously in JS with Async

Posted By : Neeraj Kumar | 27-Feb-2018

For all code examples provided first attach the following code with all samples:-

var async = require('async');

var fs = require('fs-extra');

 

1. Async each

Syntax:-

async.each(collection, iteratee, callback)

 

Async each provides a functionality to apply a function (iteratee) over each element of a collection (either an array or an object or iterable) in parallel and an optional callback for when the execution has finished either with the error or finished successfully.

If the iteratee function passes an error the main callback is called immediately stopping the execution of the iteration right from the point where the error was passed.

As the iteratee function is called over each item in the collection parallel, there is no guarantee of order in the execution over each item.

Example:-

var files = [ 'file1.txt', 'file2.txt', 'file3.txt'];

async.each(files, function(file,  callback){
   fs.readFile(file, 'utf8', function(err, data){
       if(err){
           callback(err, null);
       } else{
           console.log(data);
           callback(null, null);
       }
   })
}, function(err, results){
   if(err){
       console.error(err);
   } else {
       console.log('all files are read.');
   }
});

 

In this example, there is an array of “files” to be iterated with the function given in the async.each call. This function will read files listed in the files array, each of files in parallel with minding the order in which they occur in the array.

Here callback(null, null) just passes the flow to the final callback handler if error occurs. This passing of control flow only passes the flow once the iteration completes after iterating and performing operations on all items.

 

Output:-

 

File 1 contents.

File 2 contents.

File 3 contents.

all files are read.

 

2. Async eachOf

 

async.eachOf(collection, key,  iteratee, callback)

 

Same as async each the only difference is that it keeps the index (in case of an array as a collection) or key (in case of object collection) from the to be iterated collection.

 

Example:-

 

var files = [ 'file1.txt', 'file2.txt', 'file3.txt' ];

async.eachOf(files, function(file, index, callback){
   fs.readFile(file, 'utf8', function(err, data){
       if(err){
           callback(err, null);
       } else{
           console.log(index  + ': '+ data);
           callback(null, null);
       }
   })
}, function(err, results){
   if(err){
       console.error(err);
   } else {
       console.log('all files are read.');
   }
});

 

Here in this example index is each corresponding index from the collection.

 

Output:-

 

0: File 1 contents.

1: File 2 contents.

2: File 3 contents.

all files are read.

 

3. Async eachSeries

 

async.eachSeries(collection, iteratee, callback)

 

Same as async.each but it perform the async function in a series one at a time.

 

Example:-

 

var files = [ 'file1.txt', 'file2.txt', 'file3.txt' ];

async.eachSeries(files, function(file, callback){
   fs.readFile(file, 'utf8', function(err, data){
       if(err){
           callback(err, null);
       } else{
           console.log(data);
           callback(null, null);
       }
   })
}, function(err, results){
   if(err){
       console.error(err);
   } else {
       console.log('all files are read.');
   }
});

 

Output:-

File 1 contents.

File 2 contents.

File 3 contents.

all files are read.

 

4. Async eachOfSeries

 

Same as “eachOf” but it performs async function in a series and provides index of the key of each item of the collection one at a time.

 

async.eachOfSeries(collection, index, iteratee, callback)

 

Example:-

 

var files = [ 'file1.txt', 'file2.txt', 'file3.txt' ];

async.eachOfSeries(files, function(file, index, callback){
   fs.readFile(file, 'utf8', function(err, data){
       if(err){
           callback(err, null);
       } else{
           console.log(index + ": " + data);
           callback(null, null);
       }
   })
}, function(err, results){
   if(err){
       console.error(err);
   } else {
       console.log('all files are read.');
   }
});

Output:-

0: File 1 contents.
1: File 2 contents.
2: File 3 contents.
all files are read.

About Author

Author Image
Neeraj Kumar

Neeraj is a JAVA Developer who possesses skill set in: Data Structures, Core Java, Java Enterprise Edition (Servlets, JSP, Standard Java Beans), Spring (Spring-core, spring-MVC, spring-Data-Access, Spring-Data-JPA), Hibernate, JPA, HTML, CSS, JavaScri

Request for Proposal

Name is required

Comment is required

Sending message..