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

90
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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