TrueXPixels TrueXPixels - 4 months ago 9
Node.js Question

Why is some of my code running before other ones?

So i was running my script and i was wondering why some of my code was running before eachother.

For example, i had a function, and i called it last, AFTER a message was supposed to be displayed. But instead it ran the function first THEN displayed the message.

Also, i was noticing with displaying messages, it would display in a random order each time. (Wierd Huh.)

I managed to fix some of this by creating a bunch of .this's, but i don't want to do that so much in the future as it makes code very confusing.

Here is a snippet of some of my code that this is happening to:

else if (input.startsWith('CALL')) {

//First
bot.sendMessage(message, ':arrow_right: Private Messaging You To Decrease Spam :arrow_left:');
//Second
bot.sendMessage(user.id, ':rotating_light: This feature is experimental. You may experience bugs/glitches when using it. :rotating_light: \n').then(() => getUser()); // << Third
var senderId = message.sender.id; // << Fourth

function getUser() {
try {
var userList = []
for (var user of bot.users) {
if (user.status == "online" && user.bot == false) {
userList.push(user)
}
}

bot.sendMessage(senderId, ':telephone: :arrow_right: Users Found That Meet Search Criteria: ' + userList.length + ' out of ' + bot.users.length + '.'); //Fifth
bot.sendMessage(message, ':telephone: :arrow_right: User Found: ' + userList[Math.floor(Math.random() * userList.length)]); //Sixth

} catch (err) {
bot.sendMessage(message, err)
}
console.log(userList);
console.log(userList.length);
}

}


The code works, it's just the order i was worried about.

Thanks in advance for the help :)

EDIT: I added comments to say what the order should be.

EDIT #2: Clarified Question: I was wondering how i would be able to make the code run in a certain order. And wait for a code prior to run first.

Answer

I assume bot.sendMessage makes a network request of some sort to some other system, and the issue you're observing is that those network requests seem to take effect in an unpredictable order. This makes sense, because you're not waiting for one request to finish before starting the next one.

Imagine you opened a web browser and opened two tabs to two different URLs at the same time. Which page would load first? That's basically what your code is doing.

Because I see a .then in one place, I'm going to further assume that bot.sendMessage returns a promise. So go ahead and use the promise to wait for the first request to finish before starting the next one.

Specifically, change this:

bot.sendMessage(senderId, ':telephone: :arrow_right: Users Found That Meet Search Criteria: ' + userList.length + ' out of ' + bot.users.length + '.');
bot.sendMessage(message, ':telephone: :arrow_right: User Found: ' + userList[Math.floor(Math.random() * userList.length)]);

to this:

bot.sendMessage(senderId, ':telephone: :arrow_right: Users Found That Meet Search Criteria: ' + userList.length + ' out of ' + bot.users.length + '.').then(() => {
    bot.sendMessage(message, ':telephone: :arrow_right: User Found: ' + userList[Math.floor(Math.random() * userList.length)]);
});

(Ditto with your other messages that you want to happen in a certain order.)

Comments