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

91
Visualizações
how to wait for promise to resolve in Javascript
const findNum = async ()=>{
                setTimeout(()=>{
                    return 5;
                },1000)};

const getNum = async ()=>{
                    const  num = await findNum();
                    return num;
                    }
                    
 const number =  getNum();

console.log(number);

number.then((result)=>{
                    console.log(result);
                    });

I tried so many answers from stack overflow,

like the "then" keyword, but it doesn't work!

The result I got was always like this or just some errors

Promise { <pending> }
undefined

how could I get the result out of the Promise?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

The problem you have is that setTimeout has an callback and you return the value to the callback, not to findNum

setTimeout(()=>{
   return 5;
},1000)};

As you can see, there is an anonymous arrow function that you return you value to it.

The problem here is, you cant really make this work with async / await, you should use new Promise here:

const findNum = ()=> new Promise(res => setTimeout(res, 1000, 5))

This should work.

Also, async functions are always returning promises no matter what

about 4 years ago · Santiago Gelvez 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