Jul 20, 2017 · 1 min read
Thanks for your response. Yes you are correct. As we saw in the timers and immediates section, execution order of timer with zero timeout and the immediate can depend various performance facts. Here, there’s a set of next tick callbacks executed with a set of console.log statements which buys some time for timer callbacks to be enqueued in the expired timers queue. If you comment out the process.nextTick statements, you’ll see it.
setImmediate(() => console.log(‘this is set immediate 1’));
setImmediate(() => console.log(‘this is set immediate 2’));
setImmediate(() => console.log(‘this is set immediate 3’));setTimeout(() => console.log(‘this is set timeout 1’), 0);
setTimeout(() => {
console.log(‘this is set timeout 2’);
process.nextTick(() => console.log(‘this is process.nextTick added inside setTimeout’));
}, 0);
setTimeout(() => console.log(‘this is set timeout 3’), 0);
setTimeout(() => console.log(‘this is set timeout 4’), 0);
setTimeout(() => console.log(‘this is set timeout 5’), 0);// process.nextTick(() => console.log(‘this is process.nextTick 1’));
// process.nextTick(() => {
// process.nextTick(console.log.bind(console, ‘this is the inner next tick inside next tick’));
// });
// process.nextTick(() => console.log(‘this is process.nextTick 2’));
// process.nextTick(() => console.log(‘this is process.nextTick 3’));
// process.nextTick(() => console.log(‘this is process.nextTick 4’));
Output of the above code is non-deterministic and can produce the following output too.
this is set immediate 1
this is set immediate 2
this is set immediate 3
this is set timeout 1
this is set timeout 2
this is set timeout 3
this is set timeout 4
this is set timeout 5
this is process.nextTick added inside setTimeoutIf you run the above program multiple times, you will get different outputs.
