Task
@ripl/core
Cancellable promise with AbortController integration.
Overview
Classes: TaskAbortError · Task
Type Aliases: TaskResolve · TaskReject · TaskAbortCallback · TaskExecutor
TaskAbortError class
Error thrown when a task is aborted, carrying the abort reason.
class TaskAbortError extends ErrorConstructor:
Parameters:
| Name | Type | Description |
|---|---|---|
reason? | any |
Properties:
| Property | Type | Description |
|---|---|---|
reason | any | |
name | string | |
message | string | |
stack | string | undefined | |
cause | unknown |
Task class
A cancellable promise with AbortController integration, supporting abort callbacks and chaining.
class Task<TResult = void> extends Promise<TResult>Constructor:
Parameters:
| Name | Type | Description |
|---|---|---|
executor | TaskExecutor<TResult> | |
controller? | AbortController |
Properties:
| Property | Type | Description |
|---|---|---|
signal | AbortSignal | The AbortSignal associated with this task's controller. |
hasAborted | boolean | Whether this task has already been aborted. |
readonly __@toStringTag@81 | string |
Methods:
abort(reason: unknown): Task<TResult>
Aborts the task with an optional reason, triggering all registered abort callbacks.
Parameters:
| Name | Type | Description |
|---|---|---|
reason | unknown |
then(onfulfilled: ((value: TResult) => TResult1 \| PromiseLike<TResult1>) \| null \| undefined, onrejected: ((reason: any) => TResult2 \| PromiseLike<TResult2>) \| null \| undefined): Promise<TResult1 \| TResult2>
Attaches callbacks for the resolution and/or rejection of the Promise.
Parameters:
| Name | Type | Description |
|---|---|---|
onfulfilled | ((value: TResult) => TResult1 | PromiseLike<TResult1>) | null | undefined | The callback to execute when the Promise is resolved. |
onrejected | ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined | The callback to execute when the Promise is rejected. |
catch(onrejected: ((reason: any) => TResult \| PromiseLike<TResult>) \| null \| undefined): Promise<TResult \| TResult>
Attaches a callback for only the rejection of the Promise.
Parameters:
| Name | Type | Description |
|---|---|---|
onrejected | ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined | The callback to execute when the Promise is rejected. |
finally(onfinally: (() => void) \| null \| undefined): Promise<TResult>
Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.
Parameters:
| Name | Type | Description |
|---|---|---|
onfinally | (() => void) | null | undefined | The callback to execute when the Promise is settled (fulfilled or rejected). |
TaskResolve type
Callback to resolve a task with a value or promise.
type TaskResolve<TResult> = (value: TResult | PromiseLike<TResult>) => void;TaskReject type
Callback to reject a task with an optional reason.
type TaskReject = (reason?: unknown) => unknown;TaskAbortCallback type
Callback invoked when a task is aborted, receiving the abort reason.
type TaskAbortCallback = (reason?: unknown) => void;TaskExecutor type
Executor function for a task, providing resolve, reject, abort registration, and the underlying AbortController.
type TaskExecutor<TResult> = (
resolve: TaskResolve<TResult>,
reject: TaskReject,
onAbort: (callback: TaskAbortCallback) => void,
controller: AbortController
) => void;