Skip to content

Latest commit

 

History

History
68 lines (61 loc) · 2.42 KB

index.adoc

File metadata and controls

68 lines (61 loc) · 2.42 KB

Promise

The @perfective/common/promise package provides functions to work with the Promise class.

Reference

Types

  • Resolvable<T> = T | PromiseLike<T> — a value that can be passed into Promise.resolve.

  • Resolve<T> = (value: Resolvable<T>) ⇒ void — a function that can called to resolve a Promise value.

  • Reject<E extends Error = Error> = (reason?: E) ⇒ void — a function called to reject a Promise with an optional reason.

    Note

    This type is stricter than the default type of the reject callback, as it requires an Error as a reason.

  • Executor<T, E extends Error = Error> = (resolve: Resolve<T>, reject: Reject<E>) ⇒ void — a callback passed into the Promise constructor.

  • OnFulfilled<T, U = T> = (value: T) ⇒ Resolvable<U> — a onFulfilled callback passed into the Promise.then() method.

  • OnRejected<T = never> = (reason: unknown) ⇒ Resolvable<T> — a onRejected passed into the Promise.then() or Promise.catch() methods.

  • Callback<T, E extends Error = Error> = (error: E | null | undefined, value: T) ⇒ void — an error-first callback.

Constructors

  • promise<T, E extends Error = Error>(executor: Executor<T, E>): Promise<T> — creates a new Promise with a given executor callback.

  • fulfilled<T>(value: Resolvable<T>): Promise<Awaited<T>> — creates a fulfilled Promise (a shortcut for the Promise.resolve() function).

    Note

    Using Promise.resolve() directly causes the @typescript-eslint/unbound-method linting error and a TS compiler error: TS2322: Type 'unknown' is not assignable to type 'T'.

  • rejected<T = never>(reason: Error): Promise<Awaited<T>> — creates a rejected Promise (a shortcut for the Promise.rejected() function).

    Note

    Using Promise.rejected() directly causes the @typescript-eslint/unbound-method linting error.

  • settled<T>(): BiFold<Resolvable<T>, Error, Promise<Awaited<T>>> — creates a BiFold pair of callbacks to wrap a value into a Promise.

  • settlement<T, E extends Error = Error>(resolve: Resolve<T>, reject: Reject<E>): Callback<T, E> — create a Callback that uses given resolve and reject functions from an executor to settle a Promise. Use settlement to promisify functions written in the error-first callback style.