NodeJS - setTimeout(fn,0) vs setImmediate(fn)

前端 未结 9 1316
暖寄归人
暖寄归人 2020-12-07 13:23

What is the difference between those two, and when will I use one over the other?

9条回答
  •  一整个雨季
    2020-12-07 13:45

    To understand them deeply please once go through the event loop phases.

    SetImmediate: It gets executed in the "check" phase. The check phase is called after the I/O phase.

    SetTimeOut: It gets executed in the "timer" phase. The timer phase is the first phase but is called after the I/O phase as well as the Check phase.

    To get the output in a deterministic manner, it will depend on which phase the event-loop is; accordingly, we can use the function out of two.

提交回复
热议问题