Search code examples
javascriptnode.jscallbackasync.jsasynccallback

Async.js each continue without callback


Why async.js each still works without calling callback

var async = require('async');

var arr = ['a', 'b', 'c', 1, 2, 3];

async.each(arr, function(item, callback) {
  console.log(item);
}, function(error) {
  if (error) console.log(error);
});

as a result I can see in terminal each array item, but as I understand that shouldn't be, until calling callback, right?


Solution

  • async.each just calls the "loop body" callback for each item in the array. If they are asynchronous, they'll run concurrently.

    You are never getting into your final callback, as your "asynchronous tasks" stay forever pending without ever calling callback. That's the actual problem with your code.

    If you don't want to see the next item until the previous one has called its callback, you should be using async.eachSeries instead.