Async.each () the final callback fails

async.each(spiders, function(){
    console.log('hi');
}, function(err) {
    if(err) { console.log(err);}
    else console.log('ok');
});

After registering β€œhello”, the asynchronous call did not make a callback and did not register β€œok” or errors.

What is wrong in my code?

+4
source share
2 answers

asyncprovides two important parameters to your function iterator: a itemand a callback. The first gives you the actual data element from the array, and the second gives you a function indicating the end of the actual method. A final callback (one that has a log ("ok")) is called when each iterator call points to its own callback.

So your code should look something like this:

async.each(spiders, function(item, callback) {
  console.log('spider: ' + item);
  callback(null);
}, function(err) {
  if (err) {
    return console.log(err);
  }
  console.log('ok');
});

The parameter nullmeans that there is no error.

, , , .

+9

async.each :

iterator(item, callback) - , arr. a callback(err), . , null.

: , , .

+2

All Articles