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

170
Visualizações
How the return value in promise inside a function works(Learning js for first time)

I recently started learning JS and saw a tutorial on promises, and during a handson exercise i tried writing my own promise.



function isEven(number){
    return new Promise((resolve,reject)=>{
        res = "";
        if(isNaN(number)){
            reject();
            return res;

        }
        else{ 
            if(number%2 == 0){
                res= "Even";
            }
            res = "odd";
            resolve();
            return res;
        }
    })
}
var num = 45
let str = isEven(num).then(()=>{
    console.log("succesfull");
}).catch(()=>{
    console.log("NAN");
})
document.getElementById('number').innerHTML = str;

and the response i got [object Promise] . I want to access the str value which is returned through function isEven , kindly someone help me to understand this concept

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your problem is that, while you're resolving/rejecting the promise under certain conditions, there are no values associated with that action. You need to pass whatever you want to grab to those methods as below:

function isEven(number){
    return new Promise((resolve,reject)=>{
        res = "";
        if(isNaN(number)){
            reject(res)
        }
        else{ 
            if(number%2 == 0){
                res= "Even";
            }
            res = "odd";
            resolve(res)
        }
    })
}
var num = 45
let str = isEven(num).then(()=>{
    console.log("succesfull");
}).catch(()=>{
    console.log("NAN");
})
document.getElementById('number').innerHTML = str;

If you use the .then on a Promise it auto assumes that said Promise will resolve, taking that resolved value as input for whatever callback function you're putting inside

about 4 years ago · Juan Pablo Isaza Relatório
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