NodeJS event loop problem

according to my understanding of the event loop mechanism of nodejs, if the poll queue is not empty in the poll phase, the queue will be traversed and the callback will be executed. When the poll queue is listed as empty, the callback of the next phase of setImmediate () will be executed.

the problem comes from the following code:
`setTimeout (() = > {
console.log (timer callback executed after "0ms")
}, 0)

readFile (".. / package.json", "utf-8", data = > {
console.log (" callback for completing the file"s first read operation")
})

setImmediate (() = > {
console.log ("immediate callback now")
})

process.nextTick (() = > {
console.log ("callback of process.nextTick")
}) `

output result:
callback of process.nextTick
timer callback executed after 0 milliseconds
immediate immediate callback
callback to complete the first read operation of the file

Why do I execute the callback of setImmediate instead of readFile first?

Sep.16,2021

because the callback time of readFile is not up yet, execute the following setImmediate first. Next time, execute
ides/event-loop-timers-and-nexttick/" rel=" nofollow noreferrer "> https://nodejs.org/en/docs/gu.


.

setImmediate belongs to the timer phase

Sorry, it's a mistake. It belongs to check

.
Menu