문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top