Node.js · Middle · Question 3 of 75
Inside an I/O callback (for example, in fs.readFile), which runs first?
fs.readFile(__filename, () => {
setTimeout(() => console.log("timeout"), 0);
setImmediate(() => console.log("immediate"));
});- 1The order is random every time
- 2
timeout, always - ✓Correct answer:
immediate, always
Explanation
After the poll phase the loop goes to the check phase, where setImmediate runs, before it comes back to timers. In the main module (outside I/O) the order of the two is not guaranteed.