Options
All
  • Public
  • Public/Protected
  • All
Menu

A Future is a variant of a Promise that takes a function to start execution at a later point, when the value is desired. It can be used to delay a computation, or to avoid it entirely if not needed.

By default, the computation begins when the first onfulfilled handler is added with then, but if the delay parameter is supplied as true an explicit call to the eval method is required.

This can be used to delay initiating a computation until a certain elapsed time (using delay) or until some event has occurred.

Type parameters

  • T

Hierarchy

  • Promise<T>
    • Future

Index

Constructors

constructor

  • new Future<T>(fn: () => T, delay?: boolean): Future<T>
  • Construct a Future.

    The supplied fn argument will be discarded once run, so any data referenced by it can be freed by the GC.

    Type parameters

    • T

    Parameters

    • fn: () => T

      The function performing the future calculation

        • (): T
        • Returns T

    • Optional delay: boolean

      true if the calculation should be delayed until an explicit call to eval.

    Returns Future<T>

Properties

Private #private

#private: any

Readonly [toStringTag]

[toStringTag]: string

Static [species]

[species]: PromiseConstructor

Static [toStringTag]

[toStringTag]: string

Methods

catch

  • catch<TResult>(onrejected?: null | ((reason: any) => TResult | PromiseLike<TResult>)): Promise<T | TResult>
  • Attaches a callback for only the rejection of the Promise.

    Type parameters

    • TResult = never

    Parameters

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

      The callback to execute when the Promise is rejected.

    Returns Promise<T | TResult>

    A Promise for the completion of the callback.

eval

  • eval<TResult1, TResult2>(onfulfilled?: null | ((value: T) => TResult1 | PromiseLike<TResult1>), onrejected?: null | ((reason: any) => TResult2 | PromiseLike<TResult2>)): T
  • Perform the calculation supplied on construction. Takes arguments like {@link Promise#then}. Any pending handlers from Promise.then(), Promise.catch(), or Promise.finally() will be also be handled as normal.

    eval is run synchronously if the supplied function is synchronous. The result is not wrapped in a Promise, but will be a Promise if that's what the supplied function returns. The supplied handlers are not run synchronously.

    Type parameters

    • TResult1 = T

    • TResult2 = never

    Parameters

    • Optional onfulfilled: null | ((value: T) => TResult1 | PromiseLike<TResult1>)
    • Optional onrejected: null | ((reason: any) => TResult2 | PromiseLike<TResult2>)

    Returns T

finally

  • finally(onfinally?: null | (() => void)): Promise<T>
  • 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 Promise<T>

    A Promise for the completion of the callback.

then

  • then<TResult1, TResult2>(onfulfilled?: null | ((value: T) => TResult1 | PromiseLike<TResult1>), onrejected?: null | ((reason: any) => TResult2 | PromiseLike<TResult2>)): Promise<TResult1 | TResult2>
  • This runs {@link Promise#then} normally. If the supplied future function has not been run, runs that first, unless delay was supplied as truthy, or unless neither onfullfilled or onrejected was supplied.

    Type parameters

    • TResult1 = T

    • TResult2 = never

    Parameters

    • Optional onfulfilled: null | ((value: T) => TResult1 | PromiseLike<TResult1>)
    • Optional onrejected: null | ((reason: any) => TResult2 | PromiseLike<TResult2>)

    Returns Promise<TResult1 | TResult2>

Static all

  • all<T>(values: Iterable<T | PromiseLike<T>>): Promise<T[]>
  • all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>
  • all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>
  • all<T1, T2, T3, T4, T5, T6, T7, T8>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>
  • all<T1, T2, T3, T4, T5, T6, T7>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>
  • all<T1, T2, T3, T4, T5, T6>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>
  • all<T1, T2, T3, T4, T5>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>]): Promise<[T1, T2, T3, T4, T5]>
  • all<T1, T2, T3, T4>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): Promise<[T1, T2, T3, T4]>
  • all<T1, T2, T3>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>
  • all<T1, T2>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>
  • all<T>(values: readonly (T | PromiseLike<T>)[]): Promise<T[]>
  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T

    Parameters

    • values: Iterable<T | PromiseLike<T>>

      An iterable of Promises.

    Returns Promise<T[]>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T1

    • T2

    • T3

    • T4

    • T5

    • T6

    • T7

    • T8

    • T9

    • T10

    Parameters

    • values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]

      An array of Promises.

    Returns Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T1

    • T2

    • T3

    • T4

    • T5

    • T6

    • T7

    • T8

    • T9

    Parameters

    • values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]

      An array of Promises.

    Returns Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T1

    • T2

    • T3

    • T4

    • T5

    • T6

    • T7

    • T8

    Parameters

    • values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]

      An array of Promises.

    Returns Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T1

    • T2

    • T3

    • T4

    • T5

    • T6

    • T7

    Parameters

    • values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]

      An array of Promises.

    Returns Promise<[T1, T2, T3, T4, T5, T6, T7]>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T1

    • T2

    • T3

    • T4

    • T5

    • T6

    Parameters

    • values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]

      An array of Promises.

    Returns Promise<[T1, T2, T3, T4, T5, T6]>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T1

    • T2

    • T3

    • T4

    • T5

    Parameters

    • values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>]

      An array of Promises.

    Returns Promise<[T1, T2, T3, T4, T5]>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T1

    • T2

    • T3

    • T4

    Parameters

    • values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]

      An array of Promises.

    Returns Promise<[T1, T2, T3, T4]>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T1

    • T2

    • T3

    Parameters

    • values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]

      An array of Promises.

    Returns Promise<[T1, T2, T3]>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T1

    • T2

    Parameters

    • values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]

      An array of Promises.

    Returns Promise<[T1, T2]>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

    Type parameters

    • T

    Parameters

    • values: readonly (T | PromiseLike<T>)[]

      An array of Promises.

    Returns Promise<T[]>

    A new Promise.

Static allSettled

  • allSettled<T>(values: T): Promise<{-readonly [ P in string | number | symbol]-?: PromiseSettledResult<T[P] extends PromiseLike<U> ? U : T[P]> }>
  • allSettled<T>(values: Iterable<T>): Promise<PromiseSettledResult<T extends PromiseLike<U> ? U : T>[]>
  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve or reject.

    Type parameters

    • T: readonly unknown[] | readonly [unknown]

    Parameters

    • values: T

      An array of Promises.

    Returns Promise<{-readonly [ P in string | number | symbol]-?: PromiseSettledResult<T[P] extends PromiseLike<U> ? U : T[P]> }>

    A new Promise.

  • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve or reject.

    Type parameters

    • T

    Parameters

    • values: Iterable<T>

      An array of Promises.

    Returns Promise<PromiseSettledResult<T extends PromiseLike<U> ? U : T>[]>

    A new Promise.

Static any

  • any<T>(values: (T | PromiseLike<T>)[] | Iterable<T | PromiseLike<T>>): Promise<T>
  • The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.

    Type parameters

    • T

    Parameters

    • values: (T | PromiseLike<T>)[] | Iterable<T | PromiseLike<T>>

      An array or iterable of Promises.

    Returns Promise<T>

    A new Promise.

Static race

  • race<T>(values: Iterable<T>): Promise<T extends PromiseLike<U> ? U : T>
  • race<T>(values: Iterable<T | PromiseLike<T>>): Promise<T>
  • race<T>(values: readonly T[]): Promise<T extends PromiseLike<U> ? U : T>
  • Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

    Type parameters

    • T

    Parameters

    • values: Iterable<T>

      An iterable of Promises.

    Returns Promise<T extends PromiseLike<U> ? U : T>

    A new Promise.

  • Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

    Type parameters

    • T

    Parameters

    • values: Iterable<T | PromiseLike<T>>

      An iterable of Promises.

    Returns Promise<T>

    A new Promise.

  • Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

    Type parameters

    • T

    Parameters

    • values: readonly T[]

      An array of Promises.

    Returns Promise<T extends PromiseLike<U> ? U : T>

    A new Promise.

Static reject

  • reject<T>(reason?: any): Promise<T>
  • Creates a new rejected promise for the provided reason.

    Type parameters

    • T = never

    Parameters

    • Optional reason: any

      The reason the promise was rejected.

    Returns Promise<T>

    A new rejected Promise.

Static resolve

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

Generated using TypeDoc