DEV Community 👩‍💻👨‍💻

Neeraj P Yetheendran
Neeraj P Yetheendran

Posted on

Can anyone provide an animation on the Node.js Event Loop?

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)

Collapse
 
leandroandrade profile image
Leandro Andrade

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:

  • phase 1: timers (setTimeout, setInterval)
  • phase 2: I/O logic
  • phase 3: polling (incoming requests)
  • phase 4: check (setImmediate)
  • phase 5: close

Into microtasks, we have:

  • process.nextTick
  • Promise

Obs.: process.nextTick has priority execution that Promise. First process.nextTick executes and then Promise 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 and process.nextTick have two different approach and applications.

I hope that helps you!!!

Take Your Github Repository To The Next Level

Take Your Github Repository To The Next Level 🚀️: A step-by-step guide on creating the perfect Github repository.