Skip to content Skip to sidebar Skip to footer

Last Callback Not Being Called Using Async

I cannot seem to get the last callback (commented as 'optional callback') called to send the result back to the browser. Any pointers as to what I am doing wrong? I am using the fo

Solution 1:

I'm not sure why you're using async since you're only calling one asynchronous function. But the reason your callback isn't called is because you're not ending the first function by calling its callback:

async.parallel([
  function(callback) {
    getUserByName(userName, function(err, user) {
      // call async callback with err and usercallback(err, user);
    });
  }
], function(err, record) {
  console.log('5. Following record has been retrieved:' + record);
  res.send(record);
});

Or, shorter:

async.parallel([
  function(callback) {
    getUserByName(callback);
  }
], function(err, record) {
  console.log('5. Following record has been retrieved:' + record);
  res.send(record);
});

Or, in this case, even shorter (and without the need for async):

getUserByName(function(err, record) {
  // handle error, or ...console.log('5. Following record has been retrieved:' + record);
  res.send(record);
});

Solution 2:

Saw my mistake, missing the line where I should have returned the callback as in:

async.parallel([
    function(callback){
        getUserByName(userName, function(err, user) {
            if (err) returncallback(err);
            record = user;
            returncallback(null, record);
        });
    }

Post a Comment for "Last Callback Not Being Called Using Async"