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,
Async each provides a functionality to apply a function (
If the
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
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
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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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