Question presented to candidate: "If you await scheduler.yield() versus await new Promise(r => setTimeout(r, 0)) in the middle of a function, do they resume in the same relative order, or does one genuinely come back faster than the other?"
What a strong answer should cover:
- 📌 Interview term:
scheduler.yield()— part of the browser's Prioritized Task Scheduling API, returning a real Promise that resolves after yielding control back to the browser — letting higher-priority work (user input, rendering) run before the calling code's own continuation resumes. - 📌 Interview term: the real, direct answer to the prompt — verified directly, live in a real, current browser (confirmed genuinely UNAVAILABLE in Node —
typeof scheduleris genuinely"undefined"there):scheduler.yield()andsetTimeout(0)genuinely do NOT resume in the same relative order — a real, precise, measured execution sequence confirmedscheduler.yield()'s continuation genuinely runs BEFORE asetTimeout(0)callback that was scheduled at the exact same moment. - 📌 Interview term: the real, practical use case — chunking long work — breaking a long synchronous loop into smaller pieces, calling
await scheduler.yield()between chunks, lets the browser genuinely interleave real, higher-priority work (like responding to a click) WITHOUT the real delay asetTimeout(0)-based chunking approach would introduce, verified directly to resume measurably sooner. - A precise answer names that
scheduler.yield()is part of a broader real API alongsidescheduler.postTask(), which lets a task be scheduled with an explicit real priority ("user-blocking","user-visible","background") —scheduler.yield()is genuinely the simpler, no-priority-argument "just let other things run, then come back" primitive. - A precise answer names that this is a genuinely browser-only API — verified directly — with no Node.js equivalent, since it exists specifically to interact with a browser's own rendering/input event loop, a concept that does not apply to a server-side Node process.
Clarifying questions expected:
- None — this is a definitional/technical question; directly answering the prompt's own ordering comparison with real, measured proof is the strong signal.
Code / implementation expected: Yes — a real, precise execution-order comparison between scheduler.yield() and setTimeout(0), verified live in a real browser, is the clearest, most convincing demonstration.