Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

373
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda