Ben*_*Ben 52 loops asynchronous object node.js node-async
我正在使用节点async lib - https://github.com/caolan/async#forEach,并希望迭代一个对象并打印出它的索引键.一旦完成,我想执行回调.
这是我到目前为止,但'iterating done'从未见过:
async.forEach(Object.keys(dataObj), function (err, callback){
console.log('*****');
}, function() {
console.log('iterating done');
});
Run Code Online (Sandbox Code Playgroud)
为什么不调用最终函数?
如何打印对象索引键?
ste*_*ewe 121
最终函数不会被调用,因为async.forEach需要callback为每个元素调用函数.
使用这样的东西:
async.forEach(Object.keys(dataObj), function (item, callback){
console.log(item); // print the key
// tell async that that particular element of the iterator is done
callback();
}, function(err) {
console.log('iterating done');
});
Run Code Online (Sandbox Code Playgroud)