The @perfective/common/promise
package provides functions to work with the
Promise
class.
-
Resolvable<T> = T | PromiseLike<T>
— a value that can be passed intoPromise.resolve
. -
Resolve<T> = (value: Resolvable<T>) ⇒ void
— a function that can called to resolve aPromise
value. -
Reject<E extends Error = Error> = (reason?: E) ⇒ void
— a function called to reject aPromise
with an optionalreason
.NoteThis type is stricter than the default type of the reject callback, as it requires an
Error
as areason
. -
Executor<T, E extends Error = Error> = (resolve: Resolve<T>, reject: Reject<E>) ⇒ void
— a callback passed into thePromise
constructor. -
OnFulfilled<T, U = T> = (value: T) ⇒ Resolvable<U>
— aonFulfilled
callback passed into thePromise.then()
method. -
OnRejected<T = never> = (reason: unknown) ⇒ Resolvable<T>
— aonRejected
passed into thePromise.then()
orPromise.catch()
methods. -
Callback<T, E extends Error = Error> = (error: E | null | undefined, value: T) ⇒ void
— an error-first callback.
-
promise<T, E extends Error = Error>(executor: Executor<T, E>): Promise<T>
— creates a newPromise
with a givenexecutor
callback. -
fulfilled<T>(value: Resolvable<T>): Promise<Awaited<T>>
— creates a fulfilledPromise
(a shortcut for thePromise.resolve()
function).NoteUsing
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 rejectedPromise
(a shortcut for thePromise.rejected()
function).NoteUsing
Promise.rejected()
directly causes the@typescript-eslint/unbound-method
linting error. -
settled<T>(): BiFold<Resolvable<T>, Error, Promise<Awaited<T>>>
— creates aBiFold
pair of callbacks to wrap a value into aPromise
. -
settlement<T, E extends Error = Error>(resolve: Resolve<T>, reject: Reject<E>): Callback<T, E>
— create aCallback
that uses givenresolve
andreject
functions from anexecutor
to settle aPromise
. Usesettlement
to promisify functions written in the error-first callback style.