Skip to content
Class

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

ts
constructor(
	parallelism: number,
): ConcurrentRunner;

Properties

errored

ts
get errored(): boolean;

Whether any function has errored. The runner is effectively bricked if this is true, by design.

inFlightCount

ts
get inFlightCount(): number;

The number of tasks currently running.

parallelism

ts
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()

ts
flush(): Promise<void>;

Waits for all currently running functions to finish. Throws if the runner is errored.

run()

ts
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.

Released under the Mozilla Public License 2.0.