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

324
Vistas
What is the order of execution/control flow of "await" in javascript?

I have this piece of code and I'm struggling to understand why the log order is "1,3,2".

(async () => {
 
  const doSomething = async () => {
    console.log("1");
  };
 
  await doSomething();
  console.log("2");

})();

console.log("3");

Async/await is implemented under the hood with promises. With promises even if a promise resolves immediately the callback within the then is added as a microtask, and the current callstack execution continues. Then once the call stack is completed the microtasks are run.

So the way I read the code, the await keyword before the doSomething() function means that the rest of the code in the function after the await will be wrapped in a then call, giving control back to the caller of the function. Thus "3" should be logged first, so the order should be "3,1,2"?

What is the control flow? What actually happens when the javascript translator comes across an await keyword?

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

0

the await keyword before the doSomething() function means that the rest of the code in the function after the await will be wrapped in a then call, giving control back to the caller of the function.

Yes:

(() => {
  const doSomething = () => {
    console.log("1");
    return Promise.resolve();
  };
 
  return doSomething().then(() => {
    console.log("2");
  });
})();

console.log("3");

Thus "3" should be logged first

No. Logging "1" is not delayed by any promise.

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