Nur Rony
Nur Rony

Reputation: 8083

How to prevent calling async callback on error

I am using async module in a project where I need to list errors of async.each function instead of calling the final callback.

async.each(userIds,function(userId, cb){
  request.get('/api/v1/' + userId, function(err, response, body){
     if(err){

     }
     //doing something with body
  });
}, function(err){
   if(err){
      logger.info('Something went wrong');
   }
});

how can I prevent calling async callback on error for above code? Thanks in advance.

Upvotes: 0

Views: 62

Answers (1)

Miguel
Miguel

Reputation: 20633

var errors = [];
async.each(userIds, function(userId, cb) {
  request.get('/api/v1/' + userId, function(err, response, body) {
     if(err){
      errors.push(err);
     }
     cb();
  });
}, function() {
   console.log(errors);
});

Upvotes: 1

Related Questions