The Scheduler
interface of the Prioritized Task Scheduling API provides the Scheduler.postTask()
method that can be used for adding prioritized tasks to be scheduled.
A Scheduler
can be accessed from the global object Window
or WorkerGlobalScope
(this.scheduler
).
If the feature is defined, an instance of this object is returned by the global this
in both workers and the main thread. The only property of the interface is the postTask()
method, which is used to post the task and returns a promise.
The code below shows a simple task that resolves with the text 'Task executing'. This text is logged on success. The code also shows a catch
block, which would be required in more complex code to handle when a task is aborted or throws an error.
if ("scheduler" in this) {
scheduler
.postTask(() => "Task executing")
.then((taskResult) => console.log(`${taskResult}`))
.catch((error) => console.error(`Error: ${error}`));
}
For more comprehensive example code see Prioritized Task Scheduling API > Examples.