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

362
Vistas
Why my async / await function is not working properly

I have next doubt, I was practicing about async/await functions stuff. I have created a simple test:

console.log('Testing async-await');

main();

async function wait(nSeconds) {
  await setTimeout(function() {
    console.log('Request finished !!');
  }, nSeconds * 1000);
}
async function main() {
  let a = await wait(5);
  console.log('end !!');

}

My idea, as long as I understand is simulate with wait(nSecond) function a waiting like a ajax request, for example. Everithing is called in main() function.

My actual output is :

enter image description here

But... souldnt be ??? :

Testing async-await
Request finished !!
end !!

Im telling to my code, when you get on line 'let a = await wait(5);' just dont continue until end of function is resolved . So what is happening here.

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

0

The function wait should look like this:

function wait(nSeconds){
    return new Promise( resolve => setTimeout( resolve, nSeconds * 1000 ) )
}

The setTimeout method does not return promise so await keyword does not work in this case.

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