Node.js的 - 使用异步LIB - async.foreach与对象 [英] Node.js - Using the async lib - async.foreach with object
问题描述
我使用的节点异步的LIB - https://github.com/caolan/async#的forEach 并想通过一个对象进行迭代并打印出它的索引键。一旦完成,我想执行的回调。
下面是我到目前为止,但迭代完成
是从未见过的:
async.forEach(Object.keys(dataObj),功能(ERR,回调){
的console.log('*****'); },函数(){
的console.log('迭代完成');
});
-
为什么最终的功能不会被调用?
-
我如何打印对象的索引键?
最后一个函数不会被调用,因为你不叫回调
迭代函数里面告诉异步这个迭代已经完成。
使用这样的:
async.forEach(Object.keys(dataObj),功能(项目,回调){
的console.log(项目); //打印键
回电话(); //告诉异步迭代器已完成},功能(错误){
的console.log('迭代完成');
});
I am using the node async lib - https://github.com/caolan/async#forEach and would like to iterate through an object and print out its index key. Once complete I would like execute a callback.
Here is what I have so far but the 'iterating done'
is never seen:
async.forEach(Object.keys(dataObj), function (err, callback){
console.log('*****');
}, function() {
console.log('iterating done');
});
Why does the final function not get called?
How can I print the object index key?
The final function does not get called because you don't call the callback
inside the iterator function to tell async that this iterator has completed.
Use something like this:
async.forEach(Object.keys(dataObj), function (item, callback){
console.log(item); // print the key
callback(); // tell async that the iterator has completed
}, function(err) {
console.log('iterating done');
});
这篇关于Node.js的 - 使用异步LIB - async.foreach与对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!