Options
All
  • Public
  • Public/Protected
  • All
Menu

Class is similar to the native promise class but works synchronously

Type parameters

  • T = unknown

Hierarchy

Implements

  • Promise<T>

Index

Constructors

Properties

[toStringTag]: "Promise"
override
fulfillHandlers: ConstrResolveHandler<unknown>[] = []

List of handlers to handle the promise fulfilling

rejectHandlers: ConstrRejectHandler[] = []

List of handlers to handle the promise rejection

state: State = State.pending

Actual promise state

value: unknown

Resolved promise value

Accessors

  • get isPending(): boolean

Methods

  • Attaches a common callback for the promise fulfilled and rejected states. The method returns a new promise with the state and value from the current. A value from the passed callback will be ignored unless it equals a rejected promise or exception.

    Parameters

    Returns default<T>

  • unwrap(): T
  • all<T>(values: T): default<{ [ K in string | number | symbol]: Awaited<T[K]> }>
  • all<T>(values: T): default<(T extends Iterable<Value<V>> ? V : unknown)[]>
  • Takes an iterable of promises and returns a single SyncPromise that resolves to an array of the results of the input promises. This returned promise will resolve when all the input's promises have been resolved or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error and will reject with this first rejection message/error.

    Type parameters

    • T: any[] | []

    Parameters

    • values: T

    Returns default<{ [ K in string | number | symbol]: Awaited<T[K]> }>

  • Type parameters

    • T: Iterable<unknown, T>

    Parameters

    • values: T

    Returns default<(T extends Iterable<Value<V>> ? V : unknown)[]>

  • allSettled<T>(values: T): default<{ [ K in string | number | symbol]: PromiseSettledResult<Awaited<T[K]>> }>
  • allSettled<T>(values: T): default<(T extends Iterable<Value<V>> ? PromiseSettledResult<V> : PromiseSettledResult<unknown>)[]>
  • Returns a promise that resolves after all the given promises have either been fulfilled or rejected, with an array of objects describing each promise's outcome.

    It is typically used when you have multiple asynchronous tasks that are not dependent on one another to complete successfully, or you'd always like to know the result of each promise.

    In comparison, the SyncPromise returned by SyncPromise.all() may be more appropriate if the tasks are dependent on each other / if you'd like to reject upon any of them reject immediately.

    Type parameters

    • T: any[] | []

    Parameters

    • values: T

    Returns default<{ [ K in string | number | symbol]: PromiseSettledResult<Awaited<T[K]>> }>

  • Type parameters

    • T: Iterable<unknown, T>

    Parameters

    • values: T

    Returns default<(T extends Iterable<Value<V>> ? PromiseSettledResult<V> : PromiseSettledResult<unknown>)[]>

  • any<T>(values: T): default<T extends Iterable<Value<V>> ? V : unknown>
  • Takes an iterable of SyncPromise objects and, as soon as one of the promises in the iterable fulfills, returns a single promise that resolves with the value from that promise. If no promises in the iterable fulfill (if all the given promises are rejected), then the returned promise is rejected with an AggregateError, a new subclass of Error that groups together individual errors.

    Type parameters

    • T: Iterable<unknown, T>

    Parameters

    • values: T

    Returns default<T extends Iterable<Value<V>> ? V : unknown>

  • race<T>(values: T): default<T extends Iterable<Value<V>> ? V : unknown>
  • Returns a promise that fulfills or rejects as soon as one of the promises from the iterable fulfills or rejects, with the value or reason from that promise

    Type parameters

    • T: Iterable<unknown, T>

    Parameters

    • values: T

    Returns default<T extends Iterable<Value<V>> ? V : unknown>

  • reject<T>(reason?: unknown): default<T>
  • Returns a SyncPromise object that is resolved with a given value.

    If the value is a promise, that promise is returned; if the value is a thenable (i.e., has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise, the returned promise will be fulfilled with the value.

    This function flattens nested layers of promise-like objects (e.g., a promise that resolves to a promise that resolves to something) into a single layer.

    Type parameters

    • T = unknown

    Parameters

    Returns default<T>

  • Returns a new resolved SyncPromise object with an undefined value

    Returns default<void>