【问题标题】:Node.js - Using the async lib - async.foreach with objectNode.js - 使用异步库 - async.foreach 与对象
【发布时间】:2012-05-10 12:40:17
【问题描述】:

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

这是我目前所拥有的,但从未见过 'iterating done'

    async.forEach(Object.keys(dataObj), function (err, callback){ 
        console.log('*****');

    }, function() {
        console.log('iterating done');
    });  
  1. 为什么最后的函数没有被调用?

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

【问题讨论】:

    标签: node.js loops object asynchronous node-async


    【解决方案1】:

    最终函数不会被调用,因为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');
    });  
    

    【讨论】:

    • 谢谢@stewe 我知道这很简单!问候,本。
    • 上面的代码不是每次迭代都调用callback函数吗?是否应该类似于 count++; if(dataObj.length == count) callback(); 在调用 async.forEach 循环之前声明新变量 var count = 1; ??
    • 非常感谢!
    • @AyazPasha async 为您处理。它为数组中的每个元素创建一个回调,并且在知道迭代器函数处理完每个元素之前不会调用最终用户提供的回调。
    • 这是正确的想法,但你应该更清楚你的语言,特别是注释“//告诉异步迭代器已完成”。触发回调不会告诉 async 迭代器已完成;它只是告诉 async 迭代器的特定元素已完成。最终的回调只有在所有回调都被触发后才会被调用。
    【解决方案2】:

    async.each 是由 Async Lib 提供的非常有用且强大的功能。它有 3 个字段 1个集合/数组 2-迭代 3-回调 集合是指对象的数组或集合,迭代是指每次迭代,回调是可选的。 如果我们提供回调,那么它将返回您想在前端显示的响应或说出结果

    将函数 iteratee 并行应用于 coll 中的每个项目。使用列表中的项目调用 iteratee,并在完成时回调。如果迭代者将错误传递给它的回调,主回调(对于每个函数)会立即调用错误。

    请注意,由于此函数将 iteratee 并行应用于每个项目,因此无法保证 iteratee 函数将按顺序完成。

    例子-

     var updateEventCredit = function ( userId, amount ,callback) {
        async.each(userId, function(id, next) {
        var incentiveData = new domain.incentive({
        user_id:userId,
            userName: id.userName,
            amount: id.totalJeeneePrice,
            description: id.description,
        schemeType:id.schemeType
        });
    
        incentiveData.save(function (err, result) {
            if (err) {
                next(err);
            } else {
                     domain.Events.findOneAndUpdate({
                        user_id: id.ids
                    }, {
                        $inc: {
                            eventsCredit: id.totalJeeneePrice
                        }
                    },{new:true}, function (err, result) {
                        if (err) {
                            Logger.info("Update status", err)
                            next(err);
                        } else {
                         Logger.info("Update status", result)
                         sendContributionNotification(id.ids,id.totalJeeneePrice);
                         next(null,null);       
                        }
                    });
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-17
      • 2014-04-28
      • 2017-04-20
      • 2011-07-05
      • 1970-01-01
      • 2014-04-24
      • 2015-02-23
      相关资源
      最近更新 更多