Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

382
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda