Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
TypeScript/JavaScript - Why is this function not returning a value?

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!