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

170
Vistas
I am finding it hard to understand why the code after await does not execute
async function setTime() {
    await new Promise(resolve => setTimeout(()=>{console.log("my func is fine")}, 5000));
}

async function realFunction() {
    await setTime();
    console.log("second call!");
}

In the above code, when i call realFunction, it logs my func is fine, but it does not log second call. Can someone help me understand what am I doing wrong, and how can I get the statement to be logged?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Because you never resolver your promise. You always need to resolve or reject it.

Also async / await is pretty much useless for setTime

function setTime(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms))
}

async function realFunction() {
    await setTime(5000);
    console.log("second call!");
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

The prolbem is that you never resolve the Promise so it's pending forever.

Try to change it like this:

async function setTime() {
  await new Promise((resolve) =>
    setTimeout(() => {
      console.log('my func is fine');
      resolve();
    }, 5000)
  );
}

async function realFunction() {
  await setTime();
  console.log('second call!');
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to resolve the promise, and for that you can call resolve()

So your setTime() function would look like this:

async function setTime() {
    await new Promise(resolve => setTimeout(()=>{
        console.log("my func is fine")
        return resolve();
    }, 5000));
}
about 4 years ago · Juan Pablo Isaza 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