Node.js & Libuv•Part 2: Runtime Internals, Libuv & The Event Loop (6 - 20)
Microtasks execution relative to Macrotasks in Node.js.
In Simple Words:
- Microtasks include
process.nextTick()and Promise callbacks (.then(),await,queueMicrotask). - In Node.js, the microtask queue executes immediately after the currently running JavaScript operation finishes, between every phase transition of the event loop.
nextTickqueue runs with highest priority, followed by the standard Promise microtask queue.
javascript
Promise.resolve().then(() => console.log('Promise Microtask'));
process.nextTick(() => console.log('nextTick Microtask (runs first!)'));
console.log('Synchronous script');
// Output: Synchronous script -> nextTick Microtask -> Promise Microtask