Note that there are some explanatory texts on larger screens.

plurals
  1. POWait until forEach loop is finished?
    text
    copied!<p>Adding a callback straight after the forEach loop doesn't work. I'm new to node.js so any help would be appreciated, thanks.</p> <p>This is for a Minecraft bot that logs players login and logout times. After I get kicked from the server I want to end all players sessions before attempting to rejoin but in the following code even though im using <code>async.series</code> it still doesn't wait until the first function is finished - is this due to the position of my callback?</p> <pre><code>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); } ]); }); </code></pre> <p>...</p> <p>Where is the correct place to put the callback?</p> <pre><code>function logoutAllPlayers(timestamp, callback) { findOnlinePlayers(function(onlinePlayers) { if (onlinePlayers.length &gt; 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); } }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload