Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!