gpt4 book ai didi

javascript - 异步 js 与 Node

转载 作者:行者123 更新时间:2023-11-30 17:29:54 24 4
gpt4 key购买 nike

这个函数使用 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'函数中,return doneCallback(null) 是在每次传递新数字时运行,还是在所有数字完成后运行?

我认为它是在传递完所有数字和 console.log' 之后运行的,IMO 返回会中断并停止该功能。这是实际发生的事情吗?

最佳答案

不,doneCallback 发生在return 之前,因为doneCallback 的结果是函数的返回值。每次调用 square 时,都会调用一次 doneCallback

关于javascript - 异步 js 与 Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com