Skip to the solution
MediumFrontend5 min read

What is scheduler.yield() and how does it prevent main-thread blocking?

142 views

The problem

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 scheduler is genuinely "undefined" there): scheduler.yield() and setTimeout(0) genuinely do NOT resume in the same relative order β€” a real, precise, measured execution sequence confirmed scheduler.yield()'s continuation genuinely runs BEFORE a setTimeout(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 a setTimeout(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 alongside scheduler.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.

  • scheduler
  • performance
  • event-loop

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, live proof (verified in a real, current browser β€” this API is not available in Node): scheduler.yield() genuinely resumes before a setTimeout(0) callback scheduled at the same moment

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 113 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What do Array.from and Array.of do?

Continue