Microtasks, process.nextTick, and setImmediate

Node.js has multiple queues with different scheduling behavior. Promise callbacks and other microtasks can execute before the event loop proceeds to later phases. process.nextTick() has especially high priority in Node.js and should not be recursively scheduled without care because excessive use can starve I/O.

setImmediate() schedules work for the check phase, while timers such as setTimeout() execute in the timers phase when their timing conditions are met.

Scheduling Order
console.log('start');

setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));
Promise.resolve().then(() => console.log('promise'));
process.nextTick(() => console.log('nextTick'));

console.log('end');
💡 Synchronous code runs first. Node-specific scheduling rules then determine the ordering of nextTick, Promise microtasks, timers, and immediates.
Key Takeaways
  • Synchronous JavaScript runs before asynchronous callbacks.
  • Promise callbacks are microtasks.
  • process.nextTick() should be used carefully because it can starve the event loop.
Knowledge Check

Which mechanism is specifically a Node.js next-tick queue mechanism?