I watched a video on JS event loops and read the Node.js documentation on event loop, timers and process.nextTick(). Although I feel like I understood most of it, I'm still confused on some aspects like the difference between process.nextTick() and setImmediate().
I was hoping someone could provide an animation on it like the one for the JS event loop in this video: https://youtu.be/8aGhZQkoFbQ?t=1048
Oldest comments (1)
Hi, I will try to help you without animation.
Before deep diving into setImmediate and process.nextTick, we need to understand two concepts: Node.js phases and Microstasks.
We have five phases in Node.js:
Into microtasks, we have:
process.nextTick
Promise
Obs.:
process.nextTick
has priority execution thatPromise
. Firstprocess.nextTick
executes and thenPromise
executes.So, when Node.js starts running, it will pass through all of the phases, but before going to another phase, if exists microtasks, all of those will be executed.
setImmediate
andprocess.nextTick
have two different approach and applications.I hope that helps you!!!