Node.js async.js edit

async.eachTo handle array of data efficiently

When we want to handle array of data, its better to use async.each. When we want to perform something with all data & want to get the final callback once everything is done, then this method will be useful. This is handled in parallel way.

function createUser(userName, callback)
{
    //create user in db
    callback(null)//or error based on creation
}

var arrayOfData = ['Ritu', 'Sid', 'Tom'];
async.each(arrayOfData, function(eachUserName, callback) {

    // Perform operation on each user.
    console.log('Creating user '+eachUserName);
    //Returning callback is must. Else it wont get the final callback, even if we miss to return one callback
    createUser(eachUserName, callback);
  
}, function(err) {
    //If any of the user creation failed may throw error.
    if( err ) {
      // One of the iterations produced an error.
      // All processing will now stop.
      console.log('unable to create user');
    } else {
      console.log('All user created successfully');
    }
});

To do one at a time can use async.eachSeries

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Table Of Contents
2 npm
26 async.js
42 cli
60 Hack
65 ES6
92 CORS
106 N-API
  ↑ ↓ to navigate     ↵ to select     Esc to close