Difference between process.nextTick, Promise.then, queueMicrotask
1. Definition
These are all used to schedule microtasks in Node.js, but they have different priorities and execution order in the event loop.
2. Execution Priority (High → Low)
process.nextTick() Promise.then() queueMicrotask()
3. Explanation
- process.nextTick: Runs immediately after current operation (highest priority)
- Promise.then: Runs in microtask queue after nextTick
- queueMicrotask: Similar to Promise.then but slightly lower priority
4. Example
console.log("Start");
process.nextTick(() => console.log("nextTick"));
Promise.resolve().then(() => console.log("Promise"));
queueMicrotask(() => console.log("Microtask"));
console.log("End");
// Output:
// Start
// End
// nextTick
// Promise
// Microtask5. Key Differences
- process.nextTick has its own queue (runs before event loop)
- Promise.then uses microtask queue
- queueMicrotask is standard API (browser + Node)
6. Important Note
Overusing process.nextTick can block the event loop because it runs before other tasks.