A task that can be cancelled.

We recommend using the Task.from method to create a task. When creating subtasks, pass the same controller to all subtasks.

Example

const parent = Task.from((controller) => {
const child1 = Task.from(() => { ... }, controller);
const child2 = Task.from(() => { ... }, controller);
});
parent.cancel();

This will cancel all subtasks when the parent is cancelled.

Type Parameters

  • T

    The type of the task result

Constructors

Properties

Accessors

Methods

Constructors

  • Type Parameters

    • T

    Parameters

    • fn: ((controller) => Promise<T>)
        • (controller): Promise<T>
        • Parameters

          • controller: AbortController

          Returns Promise<T>

    • controller: AbortController
    • Optional name: string

    Returns Task<T>

Properties

name?: string

Accessors

Methods

  • Cancels the task and waits for it to complete.

    Parameters

    • Optional timeout: number

      The timeout in milliseconds

    Returns Promise<Completed | Aborted>

    The result status of the task (timeout, completed, aborted)

  • Creates a new task from a function.

    Type Parameters

    • T

    Parameters

    • fn: ((controller) => Promise<T>)

      The function to run

        • (controller): Promise<T>
        • Parameters

          • controller: AbortController

          Returns Promise<T>

    • Optional controller: AbortController

      The abort controller to use

    • Optional name: string

    Returns Task<T>

    A new task