A polyfill for the Scheduler API with a pre-configured progressively-enhanced function helps to split long-running tasks into chunks.
npm install post-taskA pre-configured progressively-enhancement utility function based on the
Scheduler API.
If the Scheduler API is available, use it. Otherwise set a timeout as a
fallback.
The Scheduler API relies on browser heuristics, while the fallback waits and
calls back only after a certain time has passed.
The tasks all return a Promise since the scheduler.postTask returns
one.
The interface re-exposes the values accepted for thescheduler.postTask API
and forwards them through when that API is available.
The fallbacks are configured as following:
| Priority | Timeout delay (ms) |
| ----------------- | ------------------ |
| "user-blocking" | 0 |
| "user-visible" | 0 |
| "background" | 150 |
There is one exception: if a priority of "user-blocking" is passed, and the
Scheduler API is not available, the fallback will bequeueMicrotask
if that function
is available, which it usually will be, including in Node.js.
This function is useful for breaking up chunks of work and allowing the event
loop to cycle, which is particularly important when focusing on the
Interaction to Next Paint
web vital and of course the smooth interaction which it tries to measure.
``js
import postTask from "post-task";
// ...
postTask(() => {
trackEvent("something-happened");
}, "background");
``
This package is equally available as ESM and CJS and has a single, default
export.