Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top