I’m writing a Discord bot running on NodeJS, although this question applies to numerous scenarios having nothing to do with Discord. I need to make a synchronous method (which must remain synchronous), which returns a Promise, block until the Promise is resolved. This must be accomplished in 100% pure Javascript (no JQuery, etc.)
Example…
function getServerUsers() {
//This method must be synchronous so that it can be called from numerous other synchronous methods without having to make the latters synchronous.
//The goal is to make this synchronous method block until the inner Promise is resolved (or otherwise finalized), so that its data can be synchronously returned.
//I've tried multiple strategies for this method, including inner anonymous functions, assigning the value of 'res' inside the 'then', etc.
try {
const guild = client.guilds.cache.get(CONFIG.GUILD_ID);
console.log("getServerUsers -> fetching users...");
var res;
guild.members.fetch() //fetch returns Promise<Collection<string, GuildMember>>. 'await' cannot be used here without making getServerUsers asynchronous
.then( //'then' appears to not be waiting properly
result => {
res = result;
console.log("getServerUsers -> returning: " + JSON.stringify(res));
return res;
}
)
.catch(
err => {
console.error("getServerUsers -> guild.members.fetch(): " + err);
return null;
}
);
}
catch (e) {
console.error("getServerUsers: " + e);
return null;
}
}
function exampleMethod() {
//This method must also remain synchronous, which prevents the inner use of 'await'.
try {
var users = getServerUsers();
console.log("exampleMethod -> serverUsers: " + JSON.stringify(users)); //users has not been set at this point in execution
}
catch(e) {
console.error("exampleMethod: " + e);
}
}
exampleMethod();
Expected/desired output of executing exampleMethod
(sensitive data has been replaced with X’s):
getServerUsers -> fetching users...
getServerUsers -> returning: [{"guildId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","joinedTimestamp":1714947621595,"premiumSinceTimestamp":null,"nickname":null,"pending":false,"communicationDisabledUntilTimestamp":null,"userId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","avatar":null,"flags":0,"displayName":"XXXXXX","roles":["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"],"avatarURL":null,"displayAvatarURL":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"},{"guildId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","joinedTimestamp":1714982725184,"premiumSinceTimestamp":null,"nickname":null,"pending":false,"communicationDisabledUntilTimestamp":null,"userId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","avatar":null,"flags":0,"displayName":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","roles":["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"],"avatarURL":null,"displayAvatarURL":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}]
exampleMethod -> serverUsers: [{"guildId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","joinedTimestamp":1714947621595,"premiumSinceTimestamp":null,"nickname":null,"pending":false,"communicationDisabledUntilTimestamp":null,"userId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","avatar":null,"flags":0,"displayName":"XXXXXX","roles":["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"],"avatarURL":null,"displayAvatarURL":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"},{"guildId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","joinedTimestamp":1714982725184,"premiumSinceTimestamp":null,"nickname":null,"pending":false,"communicationDisabledUntilTimestamp":null,"userId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","avatar":null,"flags":0,"displayName":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","roles":["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"],"avatarURL":null,"displayAvatarURL":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}]
Actual/undesired output of executing exampleMethod
(sensitive data has been replaced with X’s):
getServerUsers -> fetching users...
exampleMethod -> serverUsers: undefined
getServerUsers -> returning: [{"guildId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","joinedTimestamp":1714947621595,"premiumSinceTimestamp":null,"nickname":null,"pending":false,"communicationDisabledUntilTimestamp":null,"userId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","avatar":null,"flags":0,"displayName":"XXXXXX","roles":["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"],"avatarURL":null,"displayAvatarURL":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"},{"guildId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","joinedTimestamp":1714982725184,"premiumSinceTimestamp":null,"nickname":null,"pending":false,"communicationDisabledUntilTimestamp":null,"userId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","avatar":null,"flags":0,"displayName":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","roles":["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"],"avatarURL":null,"displayAvatarURL":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}]
I’ve tried multiple strategies for this method, including inner anonymous functions, assigning the value of ‘res’ inside the ‘then’, etc.