【问题标题】:How to use aysnc module in node.js to loop over two arrays如何在 node.js 中使用异步模块循环两个数组
【发布时间】:2013-01-21 19:30:13
【问题描述】:

我想遍历一个数组,然后将一些数据写入数据库。下面的代码显示了我将如何使用 for 循环以非异步方式执行此操作。我知道这不是首选的方式。

for(var x = 0;x < tt.matches.length;x++) //Match each player with a match and a playerId     from the tournament tree
    {
        if(tt.matches[x].p[0] !== -1)
        {
            var tmId = JSON.stringify(tt.matches[x].id);
            Player.update({ _id : grUpd.players[y] },{ tournamentMatchId : tmId, treeId : tt.matches[x].p[0], opponent : tt.matches[x].p[1] },{ safe : true }, function (err) {
                if(err)
                {
                    console.log(err);
                }
            });
            y++;
        }
        if(tt.matches[x].p[0] === -1)
        {
            byes++;

        }
        if(tt.matches[x].p[1] !== -1)
        {
            var tmId = JSON.stringify(tt.matches[x].id);
            Player.update({ _id : grUpd.players[y] },{ tournamentMatchId : tmId, treeId : tt.matches[x].p[1], opponent : tt.matches[x].p[0] },{ safe : true }, function (err) {
                if(err)
                {
                    console.log(err);
                }
            });
            y++;
        }
        if(tt.matches[x].p[1] === -1)
        {
            byes++;
        }
 }

然后我需要执行以下操作,再次以“传统方式”显示。

 for(var x = 0;x < plyrs.length;x++)
 {
     var nextMatch = JSON.stringify(tt.upcoming(plyrs[x].treeId)) ;
 Player.update({ _id : plyrs[x]._id },{ tournamentMatchId : nextMatch },{ safe : true }, function (err) {
        if(err)
            {
            console.log(err);
        }
    });
 }

【问题讨论】:

  • 你现在的做法有什么问题?
  • 在执行第二组数据库写入之前,我需要确保所有数据库写入都已完成。我发布的方式并不能确保第一组写入已经完成。
  • 看看github.com/caolan/async,应该会干净很多

标签: arrays node.js asynchronous


【解决方案1】:

您可以通过保留打开的 DB 调用的计数器,然后在所有调用都返回时调用程序的下一阶段来做到这一点。见下文。

不过,这种方法存在一个理论上的漏洞,即如果您的任何 Player.update() 调用在 process.nextTick 之前返回,则可能会提前触发完成条件。

var activeCalls = 0;
for(var x = 0;x < tt.matches.length;x++) //Match each player with a match and a playerId     from the tournament tree
{
    if(tt.matches[x].p[0] !== -1)
    {
        var tmId = JSON.stringify(tt.matches[x].id);
        activeCalls++;
        Player.update({ _id : grUpd.players[y] },{ tournamentMatchId : tmId, treeId : tt.matches[x].p[0], opponent : tt.matches[x].p[1] },{ safe : true }, function (err) {
            activeCalls--;
            if(err)
            {
                console.log(err);
            }
            if ( activeCalls == 0 ) doNextThing()
        });
        y++;
    }
    if(tt.matches[x].p[0] === -1)
    {
        byes++;

    }
    if(tt.matches[x].p[1] !== -1)
    {
        var tmId = JSON.stringify(tt.matches[x].id);
        activeCalls++;
        Player.update({ _id : grUpd.players[y] },{ tournamentMatchId : tmId, treeId : tt.matches[x].p[1], opponent : tt.matches[x].p[0] },{ safe : true }, function (err) {
            activeCalls--;
            if(err)
            {
                console.log(err);
            }
            if ( activeCalls == 0 ) doNextThing()
        });
        y++;
    }
    if(tt.matches[x].p[1] === -1)
    {
        byes++;
    }
}

【讨论】:

    猜你喜欢
    • 2015-05-25
    • 1970-01-01
    • 2017-05-16
    • 2018-07-29
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    相关资源
    最近更新 更多