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?

Bergi
671k162 gold badges1k silver badges1.5k bronze badges
asked Apr 29, 2014 at 1:34
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.

answered Apr 29, 2014 at 1:38
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.