Beginner LevelNode JS
What is the difference between process.nextTick() and setImmediate()?
❓ Interview Prompt: Compare process.nextTick() (microtask executed before next phase) vs setImmediate() (macrotask executed during Check phase).
1. 'process.nextTick()':
•
Schedules a callback to execute immediately after the current operation finishes, BEFORE the event loop proceeds to any phase.
•
Warning: Recursive 'process.nextTick()' calls starve the event loop, blocking I/O processing completely!
2. 'setImmediate()':
•
Schedules a callback to execute during the Check phase of the event loop after the Poll phase completes.
Code Implementation Example
javascript
console.log('Start');
setImmediate(() => {
console.log('setImmediate: Executed in Check phase');
});
process.nextTick(() => {
console.log('process.nextTick: Executed BEFORE event loop phase transition!');
});
console.log('End');💡Comparing execution priority of process.nextTick and setImmediate.
🎯 Interview Key Takeaway
process.nextTick executes before the event loop advances to the next phase; setImmediate executes in the Check phase.
#nextTick#setImmediate#Event Loop#Beginner
Back to all Node JS Questions→