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

219
Visualizações
Node resolving not-awaited promises in main script

Why is "timeout' printed by node before terminating when I execute the following code ?

const shouldNotPrint = async () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('timeout')
      resolve('foo')
    }, 300)
  })
}

shouldNotPrint()
console.log('ended')

Running the previous code with node 16.13.0 will output

> node src/index.js
ended
timeout

Is there some kind of top-level await in node waiting for all promises to resolve before ending the process ?

To clarify: I do not want to terminate Node before the end of my promise. I just want to understand, at a low level, the behavior of node and the event-loop in that case. I understand the logic of a timeout + promise and why it would be printed after "ended". The question is why it is displayed at all and why Node is waiting for it to be displayed before terminating the process.

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