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

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

问题描述

我使用的节点异步的LIB - https://github.com/caolan/async#的forEach 并想通过一个对象进行迭代并打印出它的索引键。一旦完成,我想执行的回调。

下面是我到目前为止,但迭代完成是从未见过的:

  async.forEach(Object.keys(dataObj),功能(ERR,回调){
        的console.log('*****');    },函数(){
        的console.log('迭代完成');
    });


  1. 为什么最终的功能不会被调用?


  2. 我如何打印对象的索引键?



解决方案

最后一个函数不会被调用,因为你不叫回调迭代函数里面告诉异步这个迭代已经完成。

使用这样的:

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

  1. Why does the final function not get called?

  2. 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屋!

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