Class Task<TResult, TProgress>

Represents a Promise that supports progress reporting and cancellation.

ProgressablePromise extends the standard Promise pattern by allowing consumers to:

  • Subscribe to progress updates via an event emitter.
  • Cancel the underlying asynchronous operation using a cancellation token.
  • Chain with then, catch, and finally methods, returning a new ProgressablePromise.

Example

const progressable = new ProgressablePromise<number>((progress, token) => {
return new Promise<number>((resolve, reject) => {
let count = 0;
const interval = setInterval(() => {
if (token?.isCancellationRequested) {
clearInterval(interval);
reject(new Error('Cancelled'));
return;
}
progress?.(++count);
if (count === 10) {
clearInterval(interval);
resolve(count);
}
}, 100);
});
});

progressable.on('progress', (value) => console.log('Progress:', value));

Type Parameters

  • TResult

    The type of the resolved value of the promise.

  • TProgress = unknown

    The type of the progress update values (defaults to TResult).

Hierarchy

  • Task

Implements

Constructors

  • Type Parameters

    • TResult

    • TProgress = unknown

    Parameters

    • task: ((progress?, cancellationToken?) => Promise<TResult>)
        • (progress?, cancellationToken?): Promise<TResult>
        • Parameters

          Returns Promise<TResult>

    Returns Task<TResult, TProgress>

Properties

[toStringTag]: "Task<T>" = 'Task<T>'
cancellationTokenSource: CancellationTokenSource = ...
events: EventEmitter<DefaultEventMap> = ...
taskPromise: Promise<unknown>
taskStatus: TaskStatus = TaskStatus.executing

Accessors

Methods

  • Attaches a callback for only the rejection of the Promise.

    Type Parameters

    • TResult1 = never

    Parameters

    • Optional onrejected: null | ((reason) => TResult1 | PromiseLike<TResult1>)

      The callback to execute when the Promise is rejected.

    Returns Task<TResult1, TProgress>

    A Promise for the completion of the callback.

  • Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.

    Parameters

    • Optional onfinally: null | (() => void)

      The callback to execute when the Promise is settled (fulfilled or rejected).

    Returns Task<TResult, TProgress>

    A Promise for the completion of the callback.

  • Type Parameters

    • K extends keyof TaskEvents<TResult, TProgress>

    Parameters

    • event: K
    • listener: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: TaskEvents<TResult, TProgress>[K]

          Returns void

    Returns Task<TResult, TProgress>

  • Attaches callbacks for the resolution and/or rejection of the Promise.

    Type Parameters

    • TResult1 = TResult

    • TResult2 = never

    Parameters

    • Optional onfulfilled: null | ((value) => TResult1 | PromiseLike<TResult1>)

      The callback to execute when the Promise is resolved.

    • Optional onrejected: null | ((reason) => TResult2 | PromiseLike<TResult2>)

      The callback to execute when the Promise is rejected.

    Returns Task<TResult1 | TResult2, TProgress>

    A Promise for the completion of which ever callback is executed.