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

254
Visualizações
Node.js: adding process.nextTick() call to top-level code affects setTimeout() and setImmediate() order

As explained on the official guides (https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout), if I run the following snippet, I get different order for logs, sometimes immediate first, and sometimes timeout first, i.e. the order is non-deterministic:

setImmediate(() => {
  console.log("immediate");
});

setTimeout(() => {
  console.log("timeout");
}, 0);

Output:

> node index.js
immediate
timeout

> node index.js
immediate
timeout

> node index.js
timeout
immediate

However, if I add the process.nextTick() call to the mix, unexpectedly, the order for timeout and immediate is fixed (timeout is always printed before immediate):

setImmediate(() => {
  console.log("immediate");
});

setTimeout(() => {
  console.log("timeout");
}, 0);

process.nextTick(() => {
  console.log("next tick");
});

Output:

> node index.js
next tick
timeout
immediate

> node index.js
next tick
timeout
immediate


> node index.js
next tick
timeout
immediate

I could not understand how does process.nextTick() affect the order of timeout and immediate. Can someone help me understand what is happening internally here?

over 4 years ago · Santiago Trujillo
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