Reputation: 3024
Adding a callback straight after the forEach loop doesn't work. I'm new to node.js so any help would be appreciated, thanks.
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 async.series
it still doesn't wait until the first function is finished - is this due to the position of my callback?
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);
}
]);
});
...
Where is the correct place to put the callback?
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);
}
});
}
Upvotes: 2
Views: 3126
Reputation: 161657
In logoutAllPlayers
, you are calling callback(true);
before your async.waterfall
call has finished its steps.
Instead you should call that final waterfall callback
from //end session and update online status
and then change the last line of your waterfall to have a completion callback.
async.waterfall([
// ...
], function(){
callback(true);
});
Upvotes: 5