@andranik-arakelyan/js-utilities
Version:
Javascript utilities
42 lines (41 loc) • 1.45 kB
TypeScript
/**
* A Promise Pool that manages the execution of asynchronous functions with a maximum concurrency limit.
* Functions are queued and executed in batches, ensuring no more than the specified number run simultaneously.
*/
export declare class PromisePool {
private readonly concurrency;
private running;
private queue;
/**
* Creates a new Promise Pool with the specified concurrency limit.
* @param concurrency - Maximum number of async functions that can run simultaneously
*/
constructor(concurrency: number);
/**
* Executes an async function within the concurrency limits of the pool.
* If the pool is at capacity, the function is queued until a slot becomes available.
* @param fn - The async function to execute
* @returns A promise that resolves with the result of the async function
*/
execute<T>(fn: () => Promise<T>): Promise<T>;
/**
* Processes the next task in the queue if there's available capacity.
*/
private processQueue;
/**
* Returns the current number of running tasks.
*/
get runningCount(): number;
/**
* Returns the current number of queued tasks.
*/
get queuedCount(): number;
/**
* Returns true if the pool is currently at maximum capacity.
*/
get isAtCapacity(): boolean;
/**
* Returns the maximum concurrency limit of this pool.
*/
get maxConcurrency(): number;
}