我正在尝试使用 Node 的模块“async”及其方法“each”,以便使用 array.forEach 的异步版本。问题是它没有按预期工作,因为它从不执行回调,我将它作为第三个参数传递。
代码如下:
var async = require('async');
var arr = [1,2,3,4,5,6,7,8,9,10];
async.each(arr, function(element, cb){
console.log(element);
}, function(err){
console.log("Done");
});
事实上,即使我的程序完成了对数组的循环,它也不会打印“完成”。
你有什么想法吗?
最佳答案
那是因为您没有在第二个参数中触发回调。
var async = require('async');
var arr = [1,2,3,4,5,6,7,8,9,10];
async.each(arr, function(element, cb){
console.log(element);
cb(); // You were missing this
}, function(err){
console.log("Done");
});
这会导致:
1
2
3
4
5
6
7
8
9
10
Done
关于javascript - Node 的模块 async 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28374283/