Node.js - 使用异步库 - async.foreach 与对象 [英] Node.js - Using the async lib - async.foreach with object

查看:29
本文介绍了Node.js - 使用异步库 - async.foreach 与对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用节点异步库 - https://github.com/caolan/async#forEach 并且想遍历一个对象并打印出它的索引键.完成后,我想执行回调.

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');
    });  

  1. 为什么最终的函数没有被调用?

  1. Why does the final function not get called?

如何打印对象索引键?

推荐答案

final 函数没有被调用,因为 async.forEach 要求你调用 callback 函数每个元素.

The final function does not get called because async.forEach requires that you call the callback function for every element.

使用这样的东西:

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');
});  

这篇关于Node.js - 使用异步库 - async.foreach 与对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆