DEV Community

Discussion on: setTimeout vs setImmediate vs process.nextTick

Collapse
 
andreasjakof profile image
Andreas Jakof

which of the next ticks will be first? Is it FIFO or LIFO?
According to your previous explanation I‘d say LIFO.

Collapse
 
sachingoel profile image
Sachin Goel • Edited

No It will be FIFO

Collapse
 
andreasjakof profile image
Andreas Jakof

So in your example it will be
nextTick (racer1)
nextTick (racer2)
nextTick (racer3)
...?

Thread Thread
 
sachingoel profile image
Sachin Goel

This is not going to affect the result anyway.

Thread Thread
 
andreasjakof profile image
Andreas Jakof

Not in this case, but if there are three different callbacks set this way, it might be. That's why I'm asking.
Or are they all called at the same time, if you have enough cores? Then they will be started one after another in FIFO order, but they might end in a different order, even if they are doing exactly the same, because of different workloads on different cores.

As long as they are independent it should not matter ... all hail a functional approach. But if you work on the same object with three different callbacks, this might be a question, that could become very important.

Or is all of this dependent on the browser and its JS implementation?

I clearly (luckily?) don't have enough experience with JS, as I am working mostly in the backend.

Thread Thread
 
logicmason profile image
Mark

I think they're FIFO, but I definitely wouldn't rely upon that behavior.

Re: using multiple cores, in that case you're probably running an instance of Node on each and using PM2.