Class Deferred<T>

Source
Expand description

Represents a deferred promise.

import { Deferred } from '../src/index.js';

const ok = new Deferred<number>();
const err = new Deferred<number>();
const timeout = new Deferred<number>(AbortSignal.timeout(0));

queueMicrotask(() => ok.resolve(42));
queueMicrotask(() => err.reject(new Error('oops')));
setTimeout(() => timeout.resolve(42));

console.log(await Promise.allSettled([ok, err, timeout]));

Constructors§

Source§

new Deferred<T>(signal?: AbortSignal): Deferred<T>

Accessors§

Source§

get state(): DeferredState

Gets the current state of the deferred promise.

Methods§

Source§

reject(reason?: any): void

Rejects the promise with a reason.

Source§

resolve(value: T | PromiseLike<T>): void

Resolves the promise with a value or the result of another promise.