What is the difference between microtasks and macrotasks in Node.js?
Microtasks are promise callbacks and process.nextTick, which run after the current operation and before the next macrotask. Macrotasks are setTimeout, setImmediate, and I/O callbacks, which run one per event loop phase.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Async JavaScript and Event Loop Interview Questions for Node.js
A single-threaded loop that processes callbacks from completed async operations. It has phases (timers, pending, poll, check, close) and makes Node.js non-blocking by handling I/O asynchronously through libuv.
setTimeout runs in the timers phase after a delay. setImmediate runs in the check phase, right after the poll phase. On the next tick, setImmediate usually runs before setTimeout(0) when there are no pending timers.
A Node.js-specific function that schedules a callback to run before promise microtasks, both before macrotasks. It runs first among microtasks. Use it when you need to run code before promises on the next tick.
Still have questions?
Browse all our FAQs or reach out to our support team
