2

Which all phases exactly setImmediate and setTimeout goes through.

It's not well-explained what all phases exactly setImmediate and setTimeout goes through, please see the nodeJS guide once there the Phases Overview section says that I/O callback phase is responsible for setImmediate and again in poll and check section says check phase is responsible for setImmediate. so I am a bit confused with it. IO/callback or check or both.

Can somebody please explain the life cycle of the setTimeout and setImmediate inside the event loop and how they execute.

Akhilesh Kumar
  • 9,085
  • 13
  • 57
  • 95
Atikur Rahman
  • 1,043
  • 1
  • 7
  • 14
  • possible duplicate of https://stackoverflow.com/questions/24117267/nodejs-settimeoutfn-0-vs-setimmediatefn – AbhinavD Apr 24 '18 at 06:32
  • Possible duplicate of [NodeJS - setTimeout(fn,0) vs setImmediate(fn)](https://stackoverflow.com/questions/24117267/nodejs-settimeoutfn-0-vs-setimmediatefn) – axiac Apr 24 '18 at 06:38
  • 1
    It's not well-explained what all phases exactly setImmediate and setTimeout goes through, please see the nodeJS [doc](https://nodejs.org/en/docs/guides/event-l) once there the **Phases Overview** section says that **I/O callback** phase is responsible for setImmediate and again in **poll** and **check** section says **check** phase is responsible for setImmediate. so I am a bit confused with it. **IO/callback** or **check** or both – Atikur Rahman Apr 24 '18 at 06:49

1 Answers1

0

setTimeout callbacks are executed in the timers phase whereas setImmediate callbacks are executed in the check phase.

As the timers phase comes before the check phase, setTimeout(fn,0) will be executed before setImmediate call back.

The docs also mention it, below I am quoting the poll phase in phase overview section where they have mentioned exceptions.

poll: retrieve new I/O events; execute I/O related callbacks (almost all with the exception of close callbacks, the ones scheduled by timers, and setImmediate()); node will block here when appropriate.

Sagar Chaudhary
  • 1,343
  • 7
  • 21