I've been trying to find similar questions to mine for ages now but haven't quite come across any with the same scenario as me so I'd like to post this more specific case here. I have a decent understanding about how function calls work but I can't wrap my head around why this function just doesn't seem to be returning a value. So I have this function here:
function createServerCommand(guildID: string, commandName: string) {
var commandData
try {
commandData = UTIL.loadJSON(`SlashCommands/${commandName}.json`)
} catch (error) {
console.log(`============\nWarning: Couldn't find command data with name '${commandName}'`)
return null
}
const targetGuild = client.guilds.cache.get(guildID)
let commands = targetGuild?.commands
commands?.create(commandData).then(function(command) {
console.log(`============\nCommand created in server ${guildID}\nCommand name: ${commandName}\nCommand ID: ${command.id}`)
return command.id
})
}
(sorry if it's bad code I'm fairly new to JavaScript)
And I have tested that this function does reach the return statement every time consistently. However, it doesn't seem to actually be returning it's value to where I'm calling it from. Code where I'm calling the function:
let serverData = SSCM.getServer(guildID)
let commandID = createServerCommand(guildID, commandName)
//while (typeof commandID === 'undefined') { console.log("TRYING TO WAIT") }
console.log(commandID)
if (!commandID) {
UTIL.sendCommandError(interaction)
return
}
serverData.addCommand(commandName, commandID)
No matter what the program seems to treat the "commandID" variable as undefined, and when testing it with the while statement (commented above) to essentially force the program to wait until it was defined the program just outputted "TRYING TO WAIT" forever (yet the return statement was also reached so was it just ignored?). Does anyone have a way to explain this to someone fairly new to JavaScript?