Class ThrottledPromiseAll<T, O>

A promise that throttles the number of promises running at a time.

The constructor takes PromiseOptions to initialize the constraints of the promise.

// Create a ThrottledPromiseAll that will take numbers and return numbers
const throttledPromise = new ThrottledPromiseAll<number, number>({ concurrency: 1, timeout: Duration.milliseconds(100) });

// Define a producer function that will take a number and return a promise that resolves to a number
const numberProducer = (source: number, throttledPromiseAll: ThrottledPromiseAll<number, number | undefined>): Promise<number> => Promise.resolve(source + 1);
throttledPromiseAll.add([1, 2, 3, 4, 5], numberProducer);

const numberResults = await throttledPromiseAll.all();

Type Parameters

  • T
  • O = T

Constructors

Accessors

Methods

Constructors

Accessors

  • get results(): (undefined | O)[]
  • Returns the results of the promises that have been resolved.

    Returns (undefined | O)[]

Methods

  • Add source items to the queue of promises to be resolved. Adding an item to the queue requires a producer function that will take the source item and return a promise. Each item in the can have a different producer function, as long as the producer function conforms the types of the ThrottledPromiseAll when constructed.

    Parameters

    • source: T | T[]
    • producer: ((source: T, throttledPromise: ThrottledPromiseAll<T, undefined | O>) => Promise<undefined | O>)

      the producer function that will take the source item and return a promise. The producer function signature must conform to the types of the ThrottledPromiseAll when constructed.

        • (source, throttledPromise): Promise<undefined | O>
        • Parameters

          Returns Promise<undefined | O>

    Returns void

  • Returns a promise that resolves the items present in the queue using the associated producer.

    This function will throw an error if the timeout is reached before all items in the queue are resolved (see PromiseOptions.timeout).

    Returns Promise<(undefined | O)[]>

    A promise that resolves to an array of results.