Erdi
Erdi

Reputation: 1884

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.

Upvotes: 10

Views: 23356

Answers (3)

Krishnadas PC
Krishnadas PC

Reputation: 6519

The difference can be explained with a simple example.

Consider we have 3 files 1.txt, 2.txt, 3.txt. In which we have the contents for file 2.txt which is around 1GB size all other files are simple file with some minimum file size.

A 1GB file can be generated with the following command in linux/unix

Create a 1GB file for file 2.txt dd if=/dev/zero of=2.txt count=1024 bs=1024

The directory structure used

.
├── files
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
├── index.js
├── node_modules

Do the required npm installs and try each of this code differently.

For AsyncEachSeries

let async = require('async');
const fs  = require('fs');
let files = ['./files/1.txt', './files/2.txt', './files/3.txt'];
async.eachSeries(files, function(file, outCb)
{
   fs.readFile(file, "utf8", (err, data) => {
    console.log(file);
    outCb();
  });
},
function(err)
{
  console.log('all done!!!');
});

The output will be

./files/1.txt
./files/2.txt
./files/3.txt
all done!!!

For AsyncEach

async.each(files, function(file, outCb)
{
   fs.readFile(file, "utf8", (err, data) => {
      console.log(file);
      outCb();
  });
},
function(err)
{
  console.log('all done!!!');
});

Output will be

./files/1.txt
./files/3.txt
./files/2.txt
all done!!!

Conclusion: The AsyncEachSeries wait for each operation to complete before moving into next while AsyncEach do things in parallel since the file size for 2.txt is too large we get it completed at the end only. Hope it clarifies the difference.

Upvotes: 5

Darshan
Darshan

Reputation: 1074

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){

});

Upvotes: 10

JohnnyHK
JohnnyHK

Reputation: 311835

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.

Upvotes: 16

Related Questions