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

214
Vistas
Await setTimeout function

I am new to Javascript and unable to understand the Async/Await behaviour. I have read that Await is for Promises and should not affect non-promise items. But when I used Await with setTimeout it behaved differently.

async function foo() {
  console.log('FIrst');
  await setTimeout(()=>{console.log('timeout')},1000);
  console.log('Second');
} 
foo();
console.log('Three');

//Output is
//FIrst
//Three
//Second
//timeout

But when I remove await it gives

function foo() {
  console.log('FIrst');
  setTimeout(()=>{console.log('timeout')},1000);
  console.log('Second');
} 
foo();
console.log('Three');

//Output is
//FIrst
//Second
//Three
//timeout

So my question is in first code if it's actually waiting for settimeout to finish then why is it printing Second before timeout. Shouldn't it be timeout then Second

EDIT: Some comments say it is due to microtask Queue but can you explain the flow between callback queue and Microtask Queue.

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