Skip to content

Module: lib/util/AsyncTask

Provides an abortable asynchronous task with status tracking and promise-based interface. Designed for use cases where you need to await, resolve, reject or cancel asynchronous flows manually.

Version
1.0.0
Author
Frank Kudermann - alphanull
License
MIT
Source
AsyncTask.js, line 9

Constructor

new AsyncTask()

Creates a new AsyncTask instance. The task will be in 'pending' state until resolved, rejected, or cancelled.

Members

Internal AbortController instance used for cancellation.

Type
AbortController
Source
AsyncTask.js, line 16

Abort signal to be passed to consumers (e.g. Fetch, plugin tasks, etc).

Type
AbortSignal
Source
AsyncTask.js, line 23

Promise resolve function (internal use).

Type
function
Source
AsyncTask.js, line 30

Promise reject function (internal use).

Type
function
Source
AsyncTask.js, line 37

Current status of the task: 'pending', 'resolved', 'rejected', or 'cancelled'.

Type
string
Source
AsyncTask.js, line 44

The underlying promise that will resolve, reject, or cancel according to the task's outcome.

Type
Promise<*>
Source
AsyncTask.js, line 50

Returns the AbortSignal associated with this task. Useful for passing to APIs that support cancellation.

Returns

The signal instance.

Type AbortSignal
Source
AsyncTask.js, line 115

Returns the current status of the task: 'pending', 'resolved', 'rejected', or 'cancelled'.

Returns

The current status.

Type string
Source
AsyncTask.js, line 121

Methods

resolve(value)

Resolves the task successfully. Sets status to 'resolved' and fulfills the promise.

Parameters:
Name Type Description
value *

Value with which the promise will resolve.

Source
AsyncTask.js, line 92

reject(reason)

Rejects the task with an error. Sets status to 'rejected' (or 'cancelled' if AbortError) and rejects the promise.

Parameters:
Name Type Description
reason *

Reason for rejection (error object or value).

Source
AsyncTask.js, line 98

cancel() → Promise

Cancels the task using the AbortController. Sets status to 'cancelled' and rejects the promise with an AbortError.

Returns

Returns current promise.

Type Promise
Source
AsyncTask.js, line 104