Timers, nextTick, and setImmediate

Predict scheduling order across nextTick, promise microtasks, timers, and immediates.

Lesson content

🎯 Goal By the end of this lesson, you'll understand: What are Timers? What is process.nextTick() ? What is setImmediate() ? Execution order Differences between them Real-world examples Interview questions What are Timers? Timers schedule code to run later . Node.js provides: setTimeout() setInterval() setImmediate() 1. setTimeout() Runs once after a specified delay. Example console . log( "Start" ); setTimeout (() => { console . log( "Timeout" ); }, 2000 ); console . log( "End" ); Output Start End Timeout 2. setInterval() Runs repeatedly after every specified interval. Example let count = 1 ; const id = setInterval (() => { console . log( count ++ ); if ( count > 3 ) { clearInterval ( id ); } }, 1000 ); Output 1 2 3 3. setImmediate() Schedules a callback to run in the Check phase of the Event Loop, after the current poll phase completes. Example console . log( "Start" ); setImmediate (() => { console . log( "Immediate" ); }); console . log( "End" ); Output Start End Immediate 4. process.nextTick() Schedules a callback to run immediately after the current operation , before the Event Loop continues to the next phase . Simple Definition: process.nextTick() has higher priority than timers and setImmediate() . Example console . log( "Start" ); process . nextTick(() => { console . log( "Next Tick" ); }); console . log( "End" ); Output Start End Next Tick Execution Order Example console . log( "Start" ); setTimeout (() => { console . log( "Timeout" ); }, 0 ); setImmediate (() => { console . log( "Immediate" ); }); process . nextTick(() => { console . log( "Next Tick" ); }); console . log( "End" ); Possible Output Start End Next Tick Timeout Immediate Note: At the top level, the order between setTimeout(..., 0) and setImmediate() is not guaranteed . Their order can vary depending on the execution context. process.nextTick() always runs before both. Real-Life Example 📦 Imagine you're packing an order. Finish Current Work │ ▼ process.nextTick() │ ▼ setTimeout() │ ▼ setImmediate() Comparison Table Method Runs Repeats process.nextTick() Before the Event Loop continues ❌ No setTimeout() After a delay ❌ No setInterval() Every interval ✅ Yes setImmediate() Check phase of the Event Loop ❌ No Event Loop Visualization Current Code │ ▼ process.nextTick() │ ▼ Event Loop │ ▼ Timers (setTimeout / setInterval) │ ▼ Poll Phase │ ▼ Check Phase (setImmediate) setTimeout() vs setImmediate() setTimeout() setTimeout (() => { console . log( "Timeout" ); }, 0 ); Runs in the Timers phase . setImmediate() setImmediate (() => { console . log( "Immediate" ); }); Runs in the Check phase . Where Are They Used? API Common Use setTimeout() Delay execution setInterval() Periodic tasks (e.g., polling, clocks) setImmediate() Run code after I/O callbacks process.nextTick() Execute immediately after the current operation (e.g., defer errors or initialization) Interview Question What is the output? console . log( "A" ); process . nextTick(() => console . log( "B" )); setTimeout (() => console . log( "C" ), 0 ); console . log( "D" ); Output A D B C Best Practices ✅ Use setTimeout() for delayed execution. ✅ Use setInterval() for repeating tasks (and clear it with clearInterval() when done). ✅ Use setImmediate() when you want to schedule work after I/O callbacks. ✅ Use process.nextTick() sparingly. ❌ Avoid recursive process.nextTick() calls, as they can starve the Event Loop and delay I/O. Key Points ✅ setTimeout() runs once after a delay. ✅ setInterval() runs repeatedly at a fixed interval. ✅ setImmediate() executes in the Check phase. ✅ process.nextTick() executes before the Event Loop moves to the next phase. ✅ The order between setTimeout(..., 0) and setImmediate() is context-dependent. 🧠 Easy Formula Current Code │ ▼ process.nextTick() │ ▼ Event Loop │ ▼ setTimeout() │ ▼ setImmediate() nextTick() → Highest Priority setTimeout() → After Delay setInterval() → Repeat setImmediate() → After Poll Phase Remember: ⚡ process.nextTick() = Before the Event Loop continues ⏰ setTimeout() = Run after a delay 🔁 setInterval() = Repeat after every interval 🚀 setImmediate() = Run in the Check phase, often after I/O callbacks