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

141
Visualizações
Why does this code block log to the console in an unexpected order?

I'm extremely confused why the following code returns what it does when I write it using both promise chaining and async/await syntax:

function log() {
  console.log("Hello from inside second await")
}

async function test() {
  await console.log("Hello from first await");
  await setTimeout(log, 1000);
  await console.log("Hello from third await");
}

test();
console.log('Where does this occur?')

I thought the await keyword made the code asynchronous. Why is the first await printing to the console instantly ahead of the console.log in the main thread? Additionally, why is the second await occurring after the third? Isn't the purpose of await to make sure that important data for future functions exists before they're executed? This makes no sense to me, help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The async function means it will be to executed somtime. More from javascript async functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

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