concurrent-promise-queue
Version:
Allows promises to be queued up and executed at a maximum rate defined by time or max concurrency
42 lines (41 loc) • 1.56 kB
TypeScript
export declare type PromiseSupplier<T> = () => Promise<T>;
export interface QueueOptions {
/**
* The maximum concurrency factor for the queue.
* This will throttle the number of promises that can be processed at one time.
* Defaults to 1000 if not specified.
*/
maxNumberOfConcurrentPromises?: number;
/**
* The unit of time for rate limiting, in milliseconds.
* This will decide how large the time window is that promises are throttled in.
* Defaults to 100ms if not specified.
*/
unitOfTimeMillis?: number;
/**
* The maximum number of promises to process in the rate limiting time window.
* Defaults to 1000 if not specified.
*/
maxThroughputPerUnitTime?: number;
}
export declare class ConcurrentPromiseQueue<T> {
private readonly maxNumberOfConcurrentPromises;
private readonly unitOfTimeMillis;
private readonly maxThroughputPerUnitTime;
private promisesToExecute;
private promisesBeingExecuted;
private promiseExecutedCallbacks;
private promiseCompletedTimesLog;
private reattemptTimeoutId;
constructor(options: QueueOptions);
numberOfQueuedPromises(): number;
numberOfExecutingPromises(): number;
/**
* The queue takes a function that returns a promise.
* This function will be called at the point where the promise is going to be executed.
*
* @param promiseSupplier - A function that returns a promise.
*/
addPromise(promiseSupplier: PromiseSupplier<T>): Promise<T | null>;
execute(): void;
}