3

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 (i.e):

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.

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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
 });

Comments

0

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');
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.