gpt4 book ai didi

node.js - async.forEach 不接受多个参数?

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:27 26 4
gpt4 key购买 nike

我正在使用 async.forEach 循环遍历数组并执行一些任务。一个代码片段如下。

var docs = [1, 2, 3, 4, 5], i = 0; 
async.forEach(docs, function(doc, async_callback_each){
var _name = 'doc_each_' + (++i);
console.log(_name);
async_callback_each(null, _name);
//Pass in no error and current function name, expecting to be printed.
}, function(err, result) {
console.log('async_callback_each', err, result);
})

代码的输出是

doc_each_1
doc_each_2
doc_each_3
doc_each_4
doc_each_5
async_callback_each undefined undefined

我很高兴在调用回调之前处理所有数组元素,但请注意,我无法接收传入的 _name 。它最终以未定义的形式结束。这是正常现象还是我做错了什么?

最佳答案

Applies the function iterator to each item in arr, in parallel. The iterator is called with an item from the list, and a callback for when it has finished. If the iterator passes an error to its callback, the main callback (for the each function) is immediately called with the error.

Note, that since this function applies iterator to each item in parallel, there is no guarantee that the iterator functions will complete in order.

Arguments

arr - An array to iterate over.
iterator(item, callback) - A function to apply to each item in arr. The iterator is passed a callback(err) which must be called once it has completed. If no error has occurred, the callback should be run without arguments or with an explicit null argument.
callback(err) - A callback which is called when all iterator functions have finished, or an error occurs.

关于node.js - async.forEach 不接受多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28208997/

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