1

What is the progression of this function using the async.js library?

var async = require('async');

var square = function (num, doneCallback) {
  console.log(num * num);
  // Nothing went wrong, so callback with a null error.
  return doneCallback(null);
};

// Square each number in the array [1, 2, 3, 4]
async.each([1, 2, 3, 4], square, function (err) {
  // Square has been called on each of the numbers
  // so we're now done!
  console.log("Finished!");
});

In the 'square' function, is the return doneCallback(null) ran every time a new number is passed, or is it ran after all the numbers are finished?

I think it is ran after all the numbers have been passed and console.log'd, IMO the return would interrupt and stop the function. Is this what is actually happening?

2
  • Why are you using async at all when square() is synchronous? Commented Apr 29, 2014 at 1:41
  • It's just an example I am using from a blog post I am reading and didn't fully understand how it is working. Commented Apr 29, 2014 at 1:43

1 Answer 1

2

No, the doneCallback happens before the return, because the result of the doneCallback is the function's return value. doneCallback will be called once for each time that square is invoked.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.