ConcurrentRunner
Utility class for running async functions in parallel up to a certain level of parallelism. Can be used to apply backpressure only if the concurrency level would be exceeded.
Constructor
constructor(
parallelism: number,
): ConcurrentRunner;Properties
errored
get errored(): boolean;Whether any function has errored. The runner is effectively bricked if this is true, by design.
inFlightCount
get inFlightCount(): number;The number of tasks currently running.
parallelism
parallelism: number;The maximum number of in-flight promises. You can also think of it as the "high water mark". You can set this value to dynamically change the level of parallelism.
Methods
flush()
flush(): Promise<void>;Waits for all currently running functions to finish. Throws if the runner is errored.
run()
run(
fn: () => Promise<unknown>,
): Promise<void>;Schedules an async function to be run. If the maximum allowed level of parallelism has not yet been reached, the function will be executed immediately and run() will resolve immediately. Otherwise, the function will be called as soon as any currently-running function finishes, and run() will only resolve then.
Throws if the runner is errored.