DEV Community

loading...

Discussion on: Understanding the Node.js event loop phases and how it executes the JavaScript code.

Collapse
trunghahaha profile image
trunghahaha • Edited

I think because process.nextTick is in fs.readFile block, so when event loop comes to poll phase, it has to wait for i/o task is completed before executing anything else so every callbacks in this block are put to corresponding phase queue, now, there is nothing to do more in this phase, the event loop will move to check phase and print 5 to console, next iteration when event loop comes to i/o phase, it check that i/o task is done so it prints 'process.nextTick' -> check phase( print '4') -> closing phase -> timer phase (print '3')

Collapse
dhireneng profile image
dhiren-eng

Okay ! Did not read the code carefully that's y d confusion . Thanks a lot :)

Thread Thread
lunaticmonk profile image
Sumedh Nimkarde Author

Hey! Pardon for the late reply. As trunghahaha said, process.nextTick is wrapped inside the fs.readFile, hence, the event loop gets to know about it only when the callback of fs.readFile is executed, right? Hence, such behaviour.