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

217
Visualizações
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 à 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