Explain the event loop. What's the output of setTimeout + Promise interleaved?
Event-loop order questions are the single most-asked JS interview trope. Get it wrong and it signals you've never debugged async code.
Two queues: macrotask (setTimeout, setInterval, I/O, UI events) and microtask (Promise.then, queueMicrotask, MutationObserver).
One iteration: pick ONE macrotask → run it to completion → drain ALL microtasks → render if needed → next macrotask.
Critical: after each macrotask, ALL pending microtasks run before the next macrotask or a render.
Promise chains queue microtasks. await suspends and re-enters as a microtask.
Node.js has extra phases (timers, I/O callbacks, poll, check, close) plus process.nextTick which runs before microtasks.
Each loop tick: one macrotask runs to completion, then the entire microtask queue drains, then (in browsers) render can happen. Classic puzzle: `console.log(1); setTimeout(()=>console.log(2)); Promise.resolve().then(()=>console.log(3)); console.log(4)` prints 1, 4, 3, 2 — script is a macrotask, so its sync code runs first, then microtasks (3) drain, then the next macrotask (setTimeout callback, 2). Common trap: await x inside a function makes the rest of the function a microtask, so even if x is already resolved, code after await runs after sync code in the caller. The mental model: macrotasks are coarse-grained work boundaries; microtasks are the fine-grained "finish this logical chunk before yielding".
queueMicrotask(fn) is the direct API — use when you need microtask ordering without a Promise.
Infinite microtask recursion starves rendering — tab goes unresponsive. setTimeout(fn,0) yields.
HireMe AI (即答侠) drills these puzzles as timed quizzes — the interview version is basically muscle memory.
It runs in the render phase, after microtasks and before the next macrotask. Treat it as its own category for interview purposes.
Node has explicit phases and process.nextTick (fires before microtasks). Browser has a unified loop with rendering intertwined.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free