Node.js runs your JavaScript on a single main thread. Slow work such as reading files or waiting for the network is handed to the system, and when it finishes, a callback is queued. The event loop takes queued callbacks one at a time and runs them.
The order that matters
- All synchronous code in the current script or callback runs to the end.
- Then the process.nextTick queue empties.
- Then the promise (microtask) queue empties: .then callbacks and code after await.
- Then the loop moves through its phases: timers (setTimeout, setInterval), I/O callbacks, setImmediate, close callbacks, and empties the two queues again between each callback.
console.log('1 sync start');
setTimeout(() => console.log('5 timeout'), 0);
Promise.resolve().then(() => console.log('4 promise'));
process.nextTick(() => console.log('3 nextTick'));
console.log('2 sync end');1 sync start
2 sync end
3 nextTick
4 promise
5 timeoutA detail interviewers like
In the main script, setTimeout(fn, 0) and setImmediate(fn) can run in either order, because it depends on how quickly the loop starts. Inside an I/O callback, setImmediate always runs first:
const fs = require('fs');
fs.readFile(__filename, () => {
setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));
});immediate
timeoutDo not block the loop
While a callback runs, nothing else can. A long loop or a synchronous call such as fs.readFileSync on a big file in a request handler makes every other user wait. Use the asynchronous APIs, split heavy work, or move CPU-heavy work to worker threads. Practise more in UniCircle interview prep: web development.
#nodejs#event loop#microtasks#interview
Sources and further reading
Practise this as an interview question
Written by the UniCircle team, checked by an editor on 22 Oct 2026. It is not official policy of any university. Exam patterns and rules change; confirm dates and rules on the official website.
