Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
The Difference between setTimeout, setImmediate, And process.nextTick
What’s the difference between setTimeout(callback, 0) and process.nextTick(callback)? How about Node's setImmediate(callback)?
On the surface it appears that all three functions do the same thing — they execute the callback after the current event loop, but before anything else. The natural question to ask is, why are there three different functions? Let’s run an experiment:
let racer = function() { setTimeout(() => console.log("timeout"), 0); setImmediate(() => console.log("immediate")); process.nextTick(() => console.log("nextTick")); console.log("current event loop");}
racer()
We can see from the output that these callbacks aren’t executed in the same order they were written in the source code.
[Running] node "/Users/logicmason/timeouts.js"current event loopnextTicktimeoutimmediate
[Done] exited with code=0 in 0.203 seconds
Explanation
The first one executed was process.nextTick, which puts its callback at the front of the event queue. It will execute after the code currently being executed but before any I/O events or timers.
Next is “timeout”. Since we passed setTimeout a timeout of 0, there's no additional enforced delay before its execution, and it is placed on into the timer queue during the next loop.
Finally, we have setImmediate, which is clearly not as immediate as its name suggests! Its callback is placed in the check queue of the next cycle of the event loop. Since the check queue occurs later than the timer queue, setImmediate will be slower than setTimeout 0.
All in all, the event loop looks like this:
timers -> IO -> poll -> check ->close -> timers ->Â ...
Timers: callbacks from setInterval or setTimeoutIO callbacks: callbacks from I/O eventsIdle: used internally by Node between IO and Poll phasesPoll: retrieve new I/O eventsCheck: callbacks from setImmediate execute hereClose: handle closed connections like sockets
Challenge time!
What do you expect the output of the following code in Node to be?
let racer1 = function() { setTimeout(() => console.log("timeout"), 0); setImmediate(() => console.log("immediate")); process.nextTick(() => console.log("nextTick"));}
let racer2 = function() { process.nextTick(() => console.log("nextTick")); setTimeout(() => console.log("timeout"), 0); setImmediate(() => console.log("immediate"));}
let racer3 = function() { setImmediate(() => console.log("immediate")); process.nextTick(() => console.log("nextTick")); setTimeout(() => console.log("timeout"), 0);}
racer1()racer2()racer3()
Was it what you expected?
Originally published at https://logicmason.com on April 22, 2019.
setTimeout vs setImmediate vs process.nextTick was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.