简体   繁体   中英

How do I define the callback in the Async library's “Each” function?

In Caolan's Async library, the documentation says that the each function:

Applies the function iteratee to each item in arr, in parallel. The iteratee is called with an item from the list, and a callback for when it has finished.

This example is given:

async.each(arr, iteraree, function(err){
    //Do stuff
});

My question is how is the "callback", in this case iteraree , defined? How do you declare which callback function you would like called when it has finished?

If you were able to define functions as a parameter I could see it working but you can't (ie):

async.each(openFiles, function (item, function () {console.log("done")}), function(err){
  //Do stuff
});

But how could I define that callback not inline?

If I do something like

var iteraree = function (item, callback) {};

And passed in iteraree I still do not see where I would be able to define the behavior of callback .

That iteratee function doesn't have the signature you think it does. It looks like this:

async.each(openFiles, function (item, cb){
       console.log(item);
       cb();   
    }, function(err){
      //Do stuff
 });

It receives each item and a callback function from asynchronous.each() that has to be called when you finish whatever you are doing. You don't provide the callback.

So if you are using a named function it's just:

 function foo (item, cb){
       console.log(item);
       cb();   
    }

async.each(openFiles, foo, function(err){
      //Do stuff
 });

I still do not see where I would be able to define the behavior of callback .

You don't. callback is just a parameter. async.each calls iteratee and passes a value for callback . It's your responsibility to call callback when you are done. This lets async.each know that it can continue to the next iteration.


function (item, function () {console.log("done")}) is invalid JavaScript btw. You cannot have a function definition in place of a parameter.

Here's a modified version of example from their docs with added comments

//I personally prefer to define it as anonymous function, because it is easier to see that this function will be the one iterated
async.each(openFiles, function(file, callback) {

    callback(); //this is just like calling the next item to iterate

}, then);

//this gets called after all the items are iterated or an error is raised
function then(err){
    // if any of the file processing produced an error, err would equal that error
    if( err ) {
      // One of the iterations produced an error.
      // All processing will now stop.
      console.log('A file failed to process');
    } else {
      console.log('All files have been processed successfully');
    }
}

As the following code you can see, it is a built-in function and gives us ability to throw errors according to customize limits. All its details are controlled by async module, and we should not to override it. It is a such kind of function: you will need use it, and you no need to define it.

const async = require('async');

const array = [1, 2, 3, 4, 5];

const handleError = (err) => {
    if (err) {
        console.log(err);
        return undefined;
    }
}

async.each(array, (item, cb) => {
    if (item > 2) {
        cb('item is too big');
    }
    else {
        console.log(item);
    }
}, handleError);

console log:

1
2
item is too big

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM