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

157
Vistas
Unable to track call stack of JavaScript async-await

I wrote a code and try to come up with an expected answer to understand asynchronous behavior.

I numbered the console.log lines in a way I thought it should appear, but it printed line no. 6 before line no. 5

Actual output is:

  • outside before calling 1
  • wrapper before calling 2
  • inside waiter before calling setTimeout 3
  • inside waiter after calling setTimeout 4
  • outside after calling 6
  • wrapper after calling 5
  • after 2 sec received: Hi There

I thought it would come as lines numbered in code. But line no. 5 and 6 are swapped in actual execution. can anyone explain why?

let waiter=async function (msg){
    console.log('inside waiter before calling setTimeout 3')
    setTimeout(()=>{
        console.log('after 2 sec received: ',msg)
    }, 2000);
    console.log('inside waiter after calling setTimeout 4')
}

async function wrapper(){
    console.log('wrapper before calling 2')
    await waiter('Hi There')
    console.log('wrapper after calling 5')
}

console.log('outside before calling 1')
wrapper();
console.log('outside after calling 6')
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Because wrapper is asynchronous and you are not await-ing it for it to complete, code execution continues as normal, so 6 is logged before 5.

To have the 5 logged before 6, wait for wrapper to complete with await:

console.log('outside before calling 1');
await wrapper();
console.log('outside after calling 6');
about 4 years ago · Juan Pablo Isaza Denunciar

0

The function that is calling wrapper must either return or await something in order for JavaScript to relinquish control of execution back to the event loop. The handler you provided to setTimeout is initiated by the event loop and that can't start executing until the function you're currently calling either returns or awaits.

Since you did not await the call to wrapper, this function just returns a promise, which you ignore / discard by not making use of the return value of wrapper(). And execution continues past it to your 6 waypoint before the timeout handler will execute.

If the function in which all your example code resides is async then you could do:

console.log('outside before calling 1');
await wrapper();
console.log('outside after calling 6');

If await is not available at that level, then you could use .then to have something be executed when the promise completes.

let waiter=async function (msg){
    console.log('inside waiter before calling setTimeout 3')
    setTimeout(()=>{
        console.log('after 2 sec received: ',msg)
    }, 2000);
    console.log('inside waiter after calling setTimeout 4')
}

async function wrapper(){
    console.log('wrapper before calling 2')
    await waiter('Hi There')
    console.log('wrapper after calling 5')
}

console.log('outside before calling 1');
wrapper().then(() => {
   consolelog('promise returned by wrapper is now complete 6');
});
console.log('this will still happen before 5 (call it 4b)');
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