Node.js · Middle · Question 2 of 75
What is the order of the output?
setTimeout(() => console.log("timeout"), 0);
Promise.resolve().then(() => console.log("promise"));
process.nextTick(() => console.log("nextTick"));
console.log("sync");- 1nextTick, sync, promise, timeout
- ✓Correct answer: sync, nextTick, promise, timeout
- 3sync, promise, nextTick, timeout
Explanation
Synchronous code runs first. Then Node.js drains the process.nextTick queue, then the promise microtask queue, and only then moves to the event loop phases with timers.