Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带节点的异步js_Javascript_Node.js_Async.js - Fatal编程技术网

Javascript 带节点的异步js

Javascript 带节点的异步js,javascript,node.js,async.js,Javascript,Node.js,Async.js,这个函数使用async.js库的过程是什么 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,

这个函数使用async.js库的过程是什么

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!");
});
在“square”函数中,返回doneCallback(null)是在每次传递新数字时运行,还是在所有数字完成后运行


我认为它是在所有的数字都被传递和console.log'd之后运行的,在我看来,返回将中断并停止该函数。这就是实际发生的情况吗?

否,
doneCallback
发生在
返回之前,因为
doneCallback
的结果是函数的返回值<每次调用
square
时,code>doneCallback
将被调用一次。

square()
是同步的时,为什么要使用
async
呢?这只是我在阅读的一篇博文中使用的一个示例,我并不完全理解它是如何工作的。