Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between async.each and async.eachSeries

Is async.each working as asynchronous array iterating?

Is async.eachSeries working as synchronous array iterating?(it waits response actually)

I'm asking these because both have callbacks but async.each works like asynchronous array iterating for ex:

//This is traditional way to iterate an array with callback functions in node.js
//Is this same with async.each ? i want to know it actually.

for (var i = 0; i < data.length; i++) {
 (function (i) {
  request(data[i],function(body){
   console.log(body)
  });
 })(i);

//if this codes and async.each are doing same things , 
//i know that async gives me an aert when all finished thats the difference.
like image 904
Erdi Avatar asked Feb 07 '16 19:02

Erdi


People also ask

What is async each?

async. each() applies an asynchronous function to each item in an array in parallel. Since this function applies iterator to each item in parallel, there is no guarantee that the iterator functions will complete in order. async. each(openFiles, saveFile, function(err){ });

What is the difference between async waterfall and async series?

The async. waterfall allows each function to pass on its results to the next function, while async. series passes all the task's results to the final callback.

What are the two arguments that async queue takes?

Below are the two arguments that async. queue takes as input: Task Function. Concurrency Value.

What is async parallel?

The method async. parallel() is used to run multiple asynchronous operations in parallel. The first argument to async. parallel() is a collection of the asynchronous functions to run (an array, object or other iterable).


2 Answers

Your code example is most similar to what async.each does, as all the async request calls are made at once and allowed to proceed in parallel.

The difference with async.eachSeries is that each iteration will wait for the async operation to complete before starting the next one.

like image 192
JohnnyHK Avatar answered Oct 22 '22 08:10

JohnnyHK


async.eachSeries() applies an asynchronous function to each item in an array in series.

For example, say you have a list of users, each of which needs to post its profile data to remote server log. Order matters in this case because the users in your array are sorted.

async.eachSeries(users, function(user, callback) {
  user.postProfileToServer(callback);
});

async.each() applies an asynchronous function to each item in an array in parallel.

Since this function applies iterator to each item in parallel, there is no guarantee that the iterator functions will complete in order.

async.each(openFiles, saveFile, function(err){

});
like image 23
Darshan Avatar answered Oct 22 '22 08:10

Darshan