Since event loop runs in phases and it enters the timer phase first. Why doesnt setTimeout
get executed first since setTimeout
has 0
value and the timer should be expired the first time event loop enters timers phase.
1st:
setTimeout(() => {
console.log('timeout');
}, 0);
setImmediate(() => {
console.log('immediate');
});
Output from running:
timeout
immediate
Output from running again:
immediate
timeout
2nd. This behavior is different than what I see in the following function:
setImmediate(() => {
console.log("Immediate");
});
setTimeout(() => {
console.log("Timeout");
}, 0);
process.nextTick(() => {
console.log("Nexttick");
});
console.log("Hello");
----------It always outputs setTimeout
with 0 value before setImmediate
which is what I expect before setTimeout with 0 value is an expired timer right from the start.
node AuthenticationController.js
Hello
Nexttick
Timeout
Immediate
node AuthenticationController.js
Hello
Nexttick
Timeout
Immediate
PS C:\Users\vskk0\OneDrive\Documents\nprs-backend\AuthenticationComponent>
Why does the 2nd function always prints timeout before immediate but not the 1st one?