【问题标题】:Wait until forEach loop is finished?等到 forEach 循环结束?
【发布时间】:2013-12-20 11:20:28
【问题描述】:

在 forEach 循环之后直接添加回调不起作用。我是 node.js 的新手,所以任何帮助都将不胜感激,谢谢。

这是用于记录玩家登录和注销时间的 Minecraft 机器人。在我被从服务器踢出之后,我想在尝试重新加入之前结束所有玩家会话,但是在下面的代码中,即使我使用 async.series 它仍然不会等到第一个函数完成 - 这是由于位置我的回调?

bot.on('kicked', function(reason) {
  console.log("I got kicked for", reason, "lol");
  var timestamp = getTimestamp();

    async.series([
      function(callback) {
        console.log("logging out all players");
        logoutAllPlayers(timestamp, function(finished) {
          console.log("logged out all players " + finished);
          if (finished) {
            callback();
          }
        });
      },
      function(callback) { //don't execute until previous function has run completely
        //rejoin here
        bot = mineflayer.createBot(options);
        bindlisteners(bot);
      }
    ]);
});

...

回调的正确放置位置在哪里?

function logoutAllPlayers(timestamp, callback) {
      findOnlinePlayers(function(onlinePlayers) {
          if (onlinePlayers.length > 0) {
              onlinePlayers.forEach(function(player) {
                var playerId = player.id;
                var username = player.username;

                async.waterfall([
                  function(callback) { //add logout event
                    addEvent(playerId, 2, timestamp, function(logoutEventId) {
                      console.log("[" + timestamp + "] " + "Created logout: " + logoutEventId + " for " + username + " (" + playerId +")");
                      callback(null, logoutEventId);
                    });
                  },
                  function(logoutEventId, callback) { //find players current session
                    findSession(playerId, function(sessionId, loginEventId) {
                      console.log("[" + timestamp + "] " + "Found session: " + sessionId +" for " + username + " (" + playerId +")");
                      callback(null, sessionId, loginEventId, logoutEventId); 
                    });
                  },
                  function(sessionId, loginEventId, logoutEventId, callback) { //get timestamps for login and logout events to find the duration of session
                    findEventTimestamp(loginEventId, function(loginTimestamp) {
                      findEventTimestamp(logoutEventId, function(logoutTimestamp) {
                        var difference = diffBetweenTimestamps(loginTimestamp, logoutTimestamp);
                        console.log("[" + timestamp + "] " + "Duration: " + difference + " for " + username);
                        callback(null, sessionId, logoutEventId, difference, callback);
                      }); 
                    });
                  },
                  function(sessionId, logoutEventId, difference, callback) { //end session and update online status
                    endSession(sessionId, logoutEventId, difference, function(callback) {
                      console.log("[" + timestamp + "] " + "Updated session: " + sessionId + " for " + username + " (" + playerId +")");
                      updatenOnlineStatus(playerId, false); 
                    });     
                  }
                ]);
            });
            callback(true);
          }
      });
    }

【问题讨论】:

    标签: javascript node.js async.js


    【解决方案1】:

    logoutAllPlayers 中,您正在调用callback(true); 之前您的async.waterfall 调用已完成其步骤。

    相反,您应该从 //end session and update online status 调用最后的瀑布 callback,然后更改瀑布的最后一行以进行完成回调。

    async.waterfall([
      // ...
    ], function(){
      callback(true);
    });
    

    【讨论】:

    • 我建议向该函数添加一个错误捕获,..], function(err) { callback(!!err); },除非您有更好的“假”进一步追赶。
    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2022-01-16
    • 2020-05-09
    • 2021-03-02
    • 2019-05-22
    相关资源
    最近更新 更多