scheduler.yield: Breaking Up Long Tasks
Learn how scheduler.yield lets a long task hand control back to the browser and resume first in line, with a fallback pattern for browsers without it.

A task that runs for more than 50 ms is called a long task, and while it runs the page cannot respond to a click, a keypress or a scroll. Chrome's Interaction to Next Paint metric measures how long that wait is, and long tasks are the most common cause of a poor score. The cure is to break the work into slices and give the browser a chance to handle input between them, a technique called yielding. scheduler.yield() is the purpose-built API for it: it returns a promise that resolves after the browser has had a chance to run other tasks, and unlike a setTimeout it puts your continuation at the front of the queue, so the work resumes before other waiting tasks. This post covers how it works, how to write a loop with it, the fallback for browsers that lack it and the habits that make yielding effective.
Why yielding is needed
JavaScript runs to completion. Once a function starts, the browser cannot interrupt it, so a click that arrives during a 300 ms loop waits for the loop to finish. Splitting the loop into pieces helps only if the code between the pieces actually lets the browser run. A Promise.resolve().then(...) does not, since microtasks run before the browser returns to its task queue. Yielding has to go through a macrotask.
The basic call
async function processAll(items) {
for (const item of items) {
handle(item);
await scheduler.yield();
}
}
function handle(item) {
console.log("handled", item);
}
await processAll(["a", "b", "c"]);handled a
handled b
handled cAfter each item, the function awaits scheduler.yield(), which lets pending input and rendering run before the next item. The order of the work is preserved. For a list of three, the yielding is pointless, and for a list of ten thousand it keeps the page responsive.
Why not setTimeout
The old idiom is await new Promise((resolve) => setTimeout(resolve, 0)). It yields too, and it has two costs. A timer goes to the back of the task queue, so every other queued task, including unrelated ones such as analytics callbacks, runs before your work continues. And a nested timer is clamped to a minimum delay of about 4 ms in browsers after five nested calls, so a loop that yields after every item adds that delay each time.
scheduler.yield() avoids both. The continuation is queued with a priority boost, so it runs ahead of other tasks of the same priority that were already waiting, and it is not subject to the nested timer clamp. The practical result is that a long job finishes sooner than with a timer, while input is still handled between slices.
Yielding by time, not by item
Yielding after every item makes the slices as small as possible and adds overhead. A better rule yields when a time budget is used up.
async function processWithBudget(items, budgetMs = 10) {
let sliceStart = performance.now();
for (const item of items) {
handle(item);
if (performance.now() - sliceStart >= budgetMs) {
await scheduler.yield();
sliceStart = performance.now();
}
}
}
function handle(item) {
// real work goes here
}
await processWithBudget(Array.from({ length: 5 }, (_, i) => i));
console.log("done");doneWith a 10 ms budget, each slice stays well below the 50 ms long task threshold, and the number of yields scales with how heavy each item is. If an item takes 0.01 ms, the loop yields once per thousand items. If an item takes 8 ms, it yields every second item. The budget is a trade: smaller values mean more responsive input and more overhead.
Yield before expensive work, and after the click
Where you place the yield matters as much as how often. For an event handler, give the browser a chance to paint the response to the click before you start heavy follow-up work.
button.addEventListener("click", async () => {
button.textContent = "Saving..."; // cheap visual feedback first
await scheduler.yield(); // let the browser paint it
await saveEverything(); // then the heavy part
button.textContent = "Saved";
});(the label changes to "Saving..." immediately, then to "Saved" when the work finishes)Without the yield, the label change and the heavy work run in the same task, and the browser paints only after both, so the user sees no feedback until the work finishes. This is the single most effective use of the API for interaction latency.
Priority and continuation order
scheduler.yield() inherits the priority of the task that called it, and the continuation runs before other tasks of that priority. It also works with a signal that was passed to postTask, so a cancelled task stops at its next yield point. Code that uses await scheduler.yield() in a task running under scheduler.postTask(..., { priority: "background" }) stays at background priority throughout, so a low-priority job keeps yielding to everything that matters more.
await scheduler.postTask(
async () => {
for (let i = 0; i < 3; i++) {
console.log("background step", i);
await scheduler.yield();
}
},
{ priority: "background" },
);background step 0
background step 1
background step 2A fallback for browsers without it
scheduler.yield() is available in current Chromium browsers, and support in other engines has been arriving more slowly, so write a helper that detects it.
function yieldToMain() {
if (globalThis.scheduler?.yield) {
return scheduler.yield();
}
return new Promise((resolve) => setTimeout(resolve, 0));
}
async function work() {
console.log("slice 1");
await yieldToMain();
console.log("slice 2");
}
await work();slice 1
slice 2The fallback loses the front-of-queue behavior but still yields, so correctness holds everywhere and the speed benefit appears where the API exists. Keep the helper in one module so that you can delete the fallback later.
What yielding cannot fix
A single synchronous operation that takes 200 ms cannot be split by this API, because there is no gap to yield in. A large JSON.parse, a giant regular expression or a call into a native routine blocks until it ends. For those, the options are to break the data into smaller pieces before the call, stream the parsing, or move the work to a Web Worker, where it runs on another thread and the main thread never waits.
Yielding also does not make the total work faster. It makes it interleave, and the total time can go up slightly, since each yield costs a trip through the scheduler. The goal is responsiveness, so measure input delay, not just completion time.
Verifying the effect
Record a performance profile in the browser's developer tools before and after. A long task shows up as a block with a red corner on the main thread, and after yielding you should see the same work as a row of short blocks with gaps in between. The "Interaction to Next Paint" metric in the field data, or the Web Vitals extension locally, confirms whether real interactions got quicker. If the long task is still there, something outside your loop, such as a framework re-render triggered by each step, is holding the thread.
Where to look in an existing app
Start with the interactions your users complain about, such as a menu that opens late or a filter that lags. Record a profile of that interaction, find the long task that follows the event and read which function owns most of its time. If it is a loop over data, a yield per time budget fixes it. If it is a framework re-render, the fix lies in rendering less, or splitting the update into a quick part that paints now and a slow part scheduled afterwards. Be wary of yielding in the middle of work that must be atomic, such as updating several pieces of state that must stay consistent, since other code can run between your slices and see a half-finished state. Finish the consistent update first, then yield.


