Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

317
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda