【问题标题】:Nodejs step through array and finish each step before moving onNodejs 遍历数组并完成每个步骤,然后再继续
【发布时间】:2014-12-04 12:06:05
【问题描述】:

我在处理存储在 Redis 中的队列时遇到问题。

基本上,Redis 中的队列是一个简单的 ID 数组,我想一个一个地遍历。

我当前的代码:

async.forEach(data, function(n, done) {
    redisClient.hgetall("visitor:" + n, function(err, visitor) {
        if (visitor != null) {
            agentOnlineCheck(visitor['userID'], function(online) {
                if (online == true) {
                    console.log("We are done with this item, move on to the next");
                } else {
                    console.log("We are done with this item, move on to the next");
                }
            });

        } else {
            console.log("We are done with this item, move on to the next");
        }
    });
}, function() {
    console.log("I want this to fire when all items in data are finished");
});

我使用async库,上面的var data代表一个数组如:

['232323', '232423', '23232']

我想循环遍历数组,但一次只有一个 ID。在前一个 ID 执行完所有回调之前,不要继续下一个 ID。

这有可能吗?

【问题讨论】:

标签: node.js


【解决方案1】:

您可以使用async.eachSeries 代替async.forEach

c.f.:https://github.com/caolan/async#eachSeries

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    相关资源
    最近更新 更多