Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardNodeJSevent loop phasesSingle-choice MCQ

In a single iteration of the event loop, you queue work via `setTimeout(fn, 0)` and `setImmediate(fn)` from inside an I/O callback (e.g. an `fs.readFile` callback). Why is `setImmediate`'s callback guaranteed to run before the `setTimeout` callback in this specific case, when the ordering is non-deterministic at the top level of a script?

const fs = require('fs'); fs.readFile(__filename, () => { setTimeout(() => console.log('timeout'), 0); setImmediate(() => console.log('immediate')); });