Question presented to candidate: "If I mix several console.log calls with a couple of setTimeout(fn, 0) calls and a couple of Promise.then calls, in what exact order do they all print, and why?"
What a strong answer should cover:
- Every synchronous console.log runs first, in the exact order it appears in the source -- before any queued callback runs at all.
- After the synchronous code finishes, the ENTIRE microtask queue drains -- every Promise.then callback runs, in the order those callbacks were actually scheduled, not the order the .then() calls appear if scheduling happens indirectly.
- setTimeout callbacks run only after the whole microtask queue reports empty, and among themselves, they run in the order they were scheduled.
- A chained .then() (a second .then() attached to the result of the first) is scheduled ONLY once the first .then() callback actually runs -- so a two-deep chain takes two full microtask-queue passes, not one.
- This is not a memorized fact -- it follows directly from two queues (microtask, macrotask) and one rule: the microtask queue always fully drains before the next macrotask runs.
Clarifying questions expected:
- "Should I also cover process.nextTick, or keep this to the standard browser-style microtask/macrotask model?"
Code / implementation expected: Yes -- a short, runnable snippet mixing synchronous logs, a chained .then(), a separate .then(), and two setTimeout calls, with the real observed order.