· 6 min read
scheduler.yield(): break up long tasks in JavaScript
scheduler.yield() pauses a long JavaScript task so the browser can handle input, then resumes your code first. How it works, support and fallbacks.

scheduler.yield() is a browser API that lets a long-running JavaScript function pause, hand control back to the browser so it can handle clicks, key presses and rendering, and then continue where it left off. You call await scheduler.yield() inside the loop, and the rest of your function runs in a new task that the browser schedules ahead of other queued work. It ships in Chromium browsers from version 129 and in Firefox from 142; Safari does not support it yet.
The point is responsiveness. A page that does heavy work in one go feels frozen while that work runs, and scheduler.yield() is the cleanest way so far to give the browser breathing room without losing your place in line.
What problem does scheduler.yield() solve?
The browser's main thread does one thing at a time. A task is one unit of that work: running a script, firing an event handler, a timer callback. While a task runs, nothing else on the main thread can, including responding to the user.
A long task is any task that runs for more than 50 milliseconds. During a long task, a click or key press just waits. The metric that captures this is Interaction to Next Paint (INP), which measures how long it takes from a user interaction until the browser paints the result. Long tasks are the most common cause of poor INP.
The fix is to split the work into smaller tasks and let the browser run its own work in between. Developers have done this for years with setTimeout:
function yieldToMain() {
return new Promise((resolve) => setTimeout(resolve, 0));
}
for (const item of items) {
process(item);
await yieldToMain();
}
This works, but it has a cost. A setTimeout callback goes to the back of the queue. If other scripts on the page, such as analytics, ads or third-party widgets, have tasks waiting, they all run before your work resumes. Yield a hundred times and you might wait behind other code a hundred times. That makes developers reluctant to yield at all.
How does scheduler.yield() work?
scheduler.yield() takes no arguments and returns a promise that resolves with undefined. When you await it, your function pauses and the rest of it is scheduled as a new task. The browser can then process pending input and paint before that task runs.
The difference from setTimeout is where the continuation goes. The browser's scheduler keeps queues per priority level, and a yield continuation is placed at the front of its priority's queue rather than the back. Ordinary tasks of the same priority, including ones queued with scheduler.postTask(), run after it. So you give up the main thread briefly, but you do not lose your turn.
async function processAll(items) {
for (const item of items) {
process(item);
await scheduler.yield();
}
}
A few details worth knowing:
- Continuations keep their order. A second yield does not jump ahead of one already waiting.
- The default priority is
"user-visible", which is also the default forscheduler.postTask(). - It works in Web Workers as well as on the main thread.
- Higher-priority work, such as a
"user-blocking"task, still runs before a"user-visible"continuation.
One caveat that catches people: await only pauses the function it appears in. If you put await scheduler.yield() inside an Array.prototype.forEach callback, the outer loop does not wait, so nothing is actually chunked. Use a for...of loop instead, for the same reason covered in why await inside forEach doesn't wait.
How often should you yield?
Yielding after every single item is usually wasteful. Each yield creates a new task, and if each item takes a fraction of a millisecond, you spend more time switching than working. A better pattern is to yield on a time budget: run until roughly 50 milliseconds have passed, then yield and reset the clock.
async function processAll(items) {
let deadline = performance.now() + 50;
for (const item of items) {
process(item);
if (performance.now() >= deadline) {
await scheduler.yield();
deadline = performance.now() + 50;
}
}
}
This keeps each task under the long-task threshold while doing as much work per task as possible. If the work responds to a user action, you can also yield once right after the visible update, so the browser paints feedback before the slower work continues.
How do priority and cancellation work?
scheduler.yield() has no options of its own. Instead, it inherits context from the task it runs inside. If you start work with scheduler.postTask(), any yield within that callback keeps the task's priority and its abort signal.
const controller = new TaskController({ priority: "background" });
scheduler.postTask(async () => {
for (const chunk of chunks) {
indexChunk(chunk);
await scheduler.yield(); // continues as background work
}
}, { signal: controller.signal });
// Later, if the user navigates away from the view:
controller.abort("cancel work");
A yield inside a "background" task resumes ahead of other background tasks but still behind default and high-priority work, which is what you want for something like building a search index. If the controller is aborted, the pending await scheduler.yield() rejects with the signal's reason, so the loop stops at its next yield point and you can catch the error to clean up.
A yield inside a requestIdleCallback callback inherits background priority and cannot be aborted.
How do you use it when Safari doesn't support it?
Because the API is not yet available in every major browser, it is not Baseline. The safe approach is to feature-detect and fall back to setTimeout:
function yieldToMain() {
if (globalThis.scheduler?.yield) {
return scheduler.yield();
}
return new Promise((resolve) => setTimeout(resolve, 0));
}
Checking globalThis rather than window means the same helper works inside a worker. In browsers without support you get the old back-of-the-queue behavior, which is still far better than one long task. A polyfill for the scheduler API exists, but it does not reproduce priority inheritance for yield(), so the simple fallback above is usually enough. If you use TypeScript, the @types/wicg-task-scheduling package provides the type definitions.
| Approach | Where the continuation runs | Priority control |
|---|---|---|
setTimeout(fn, 0) | Back of the task queue | None |
scheduler.postTask(fn) | Back of its priority queue | Yes, per task |
await scheduler.yield() | Front of its priority queue | Inherited from the enclosing task |
Key takeaways
await scheduler.yield()pauses a long task so the browser can handle input and paint, then resumes your code ahead of other queued tasks.- Yield on a time budget of about 50 milliseconds rather than after every item.
- Inside
scheduler.postTask(), yields inherit the task's priority and abort signal. - It ships in Chromium 129+ and Firefox 142+; feature-detect and fall back to
setTimeoutelsewhere. - Use
for...of, notforEach, when yielding inside a loop.
FAQ
Is scheduler.yield() the same as setTimeout(0)?
Both split work into separate tasks, but setTimeout puts your continuation at the back of the queue. scheduler.yield() puts it at the front of its priority level, so other scripts cannot keep cutting in line.
Does scheduler.yield() make my code run faster?
No. Total work stays the same, and frequent yields add a little overhead. What improves is responsiveness, because user input no longer waits for your whole job to finish.
Can I use scheduler.yield() in Node.js?
It is a browser API defined for windows and workers, not part of Node.js. In Node, setImmediate is the usual way to let the event loop process other work between chunks.