TaskController
Prioritized Task Scheduling API
Prioritized Task Scheduling API
The **`TaskController`** interface of the [Prioritized Task Scheduling API](/docs/Web/API/Prioritized_Task_Scheduling_API) represents a controller object that can be used to both abort and change the [priority](/docs/Web/API/Prioritized_Task_Scheduling_API#task_priorities) of one or more prioritized tasks.
If there is no need to change task priorities, then AbortController can be used instead.
A new `TaskController` instance is created using the TaskController() constructor, optionally specifying a [priority](/docs/Web/API/Prioritized_Task_Scheduling_API#task_priorities) for its associated signal (a TaskSignal).
If not specified, the signal will have a priority of [`"user-visible"`](/docs/Web/API/Prioritized_Task_Scheduling_API#user-visible) by default.
The controller's signal can be passed as an argument to the Scheduler.postTask() method for one or more tasks.
For [mutable tasks](/docs/Web/API/Prioritized_Task_Scheduling_API#mutable_and_immutable_task_priority) (only) the task is initialized with the signal priority, and can later be changed by calling TaskController.setPriority().
For immutable tasks, any priority initialized or set by the controller is ignored.
Tasks can be aborted by calling abort() on the controller.
## Constructor
- TaskController()
- : Creates a new `TaskController` object, optionally specifying the priority of its associated [`signal`](#taskcontroller.signal).
## Instance properties
_This interface also inherits the properties of its parent, AbortController._
- `TaskController.signal`
- : Returns a TaskSignal object instance.
The signal is passed to tasks so that they can be aborted or re-prioritized by the controller.
The property is inherited from [`AbortController`](/docs/Web/API/AbortController/signal).
## Instance methods
_This interface also inherits the methods of its parent, AbortController._
- TaskController.setPriority()
- : Sets the priority of the controller's [`signal`](#taskcontroller.signal), and hence the priority of any tasks with which it is associated.
This notifies observers of the priority change by dispatching a [`prioritychange`](/docs/Web/API/TaskSignal/prioritychange_event) event.
## Examples
> [!NOTE]
> Additional "live" examples can be found in: [Prioritized Task Scheduling API Examples](/docs/Web/API/Prioritized_Task_Scheduling_API#examples).
First we create a task controller, setting the priority of its associated signal to `user-blocking`.
```js
// Create a TaskController, setting its signal priority to 'user-blocking'
const controller = new TaskController({ priority: "user-blocking" });
```
We then add an event listener for [`prioritychange`](/docs/Web/API/TaskSignal/prioritychange_event) events (here `addEventListener()` is called, but we could instead assign a handler to `TaskSignal.onprioritychange`).
The handler uses [previousPolicy](/docs/Web/API/TaskPriorityChangeEvent/previousPriority) on the event to get the original priority and TaskSignal.priority on the event target to get the new priority.
```js
// Listen for 'prioritychange' events on the controller's signal.
controller.signal.addEventListener("prioritychange", (event) => {
const previousPriority = event.previousPriority;
const newPriority = event.target.priority;
console.log(`Priority changed from ${previousPriority} to ${newPriority}.`);
});
```
We can also listen for [`abort`](/docs/Web/API/AbortSignal/abort_event) events as shown below.
This same approach would be used if the controller was an `AbortController`.
```js
controller.signal.addEventListener(
"abort",
(event) => {
console.log("Task aborted");
},
{ once: true },
);
```
Next we post the task, passing the controller signal in the optional argument.
In this case the task is just an arrow function that resolves the promise by returning some text.
We use `then` and `catch` to handle when the task resolves or is rejected, logging the return text or the error in each case.
Note that in a later code block we abort the task, so only the `catch()` block will actually be run!
```js
// Post task using the controller's signal.
// The signal priority sets the initial priority of the task
scheduler
.postTask(() => "Task execute", { signal: controller.signal })
.then((taskResult) => {
console.log(`${taskResult}`);
}) // Aborted (won't run)
.catch((error) => {
console.log(`Catch error: ${error}`);
}); // Log error
```
We can use the controller to manage the task.
Here we can change the priority using TaskController.setPriority().
This will trigger the associated `prioritychange` event.
```js
// Change the priority to 'background' using the controller
controller.setPriority("background");
```
Finally, the task can be aborted by calling abort() on the controller.
```js
// Abort the task
controller.abort();
```
The console output of this example would be:
```plain
The priority changed from user-blocking to background.
Task aborted
Catch error: AbortError
```
## Specifications
## Browser compatibility
Cite this
"TaskController". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/TaskController JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy