Skip to main content

Throttler

@webda/utils


Class: Throttler

Defined in: packages/utils/src/throttler.ts:38

Promise Throttler

Allow you to queue promise and execute them concurrently

Several libraries does that

The queue method still gives you a simple Promise linked to the resolution of your queued item, so you can still wait for the execution of the specific item

Constructors​

Constructor​

new Throttler(concurrency?, failFast?): Throttler

Defined in: packages/utils/src/throttler.ts:60

Parameters​

concurrency?​

number = 10

Maximum number of promises to execute concurrently (default 10).

failFast?​

boolean = true

When true (default), reject all pending items immediately on the first failure.

Returns​

Throttler

Properties​

_failed​

protected _failed: Error = null

Defined in: packages/utils/src/throttler.ts:54

If one promise has failed and we are in fail fast mode


_queue​

protected _queue: ThrottlerItem[] = []

Defined in: packages/utils/src/throttler.ts:42

Current queue


_waiters​

protected _waiters: [() => void, (err) => void][] = []

Defined in: packages/utils/src/throttler.ts:50

Resolver for each call to waitForCompletion


concurrency​

protected concurrency: number = 10

Defined in: packages/utils/src/throttler.ts:61

Maximum number of promises to execute concurrently (default 10).


current​

current: number = 0

Defined in: packages/utils/src/throttler.ts:46

Number of promises in-progress


failFast​

protected failFast: boolean = true

Defined in: packages/utils/src/throttler.ts:62

When true (default), reject all pending items immediately on the first failure.

Methods​

add()​

protected add(): void

Defined in: packages/utils/src/throttler.ts:184

Internal: attempt to start the next queued item if concurrency slots are available. Also resolves or rejects all waiters when the queue drains.

Returns​

void


execute()​

execute(method, name?): Promise<any>

Defined in: packages/utils/src/throttler.ts:85

Alias for queue. Queue and execute one or more promise-returning functions.

Parameters​

method​

(() => Promise<any>) | () => Promise<any>[]

A single executor or an array of executors.

name?​

string = ...

Optional name for the task, useful with getInProgress (default: Promise_N).

Returns​

Promise<any>

A Promise (or Promise.all) linked to the queued item(s).


flushWaiters()​

flushWaiters(): void

Defined in: packages/utils/src/throttler.ts:174

Flush all waiters

Returns​

void


getInProgress()​

getInProgress(): ThrottlerItem[]

Defined in: packages/utils/src/throttler.ts:141

Return all items that are currently executing (i.e. have an active Promise).

Returns​

ThrottlerItem[]

An array of in-progress ThrottlerItem instances.


getSize()​

getSize(): number

Defined in: packages/utils/src/throttler.ts:150

Return the total number of items in the queue (both queued and in-progress).

Returns​

number

The queue length.


queue()​

queue(method, name?): Promise<any>

Defined in: packages/utils/src/throttler.ts:99

Queue one or more promise-returning functions for concurrent execution.

Parameters​

method​

(() => Promise<any>) | () => Promise<any>[]

A single executor or an array of executors.

name?​

string = ...

Optional name for the task, useful with getInProgress (default: Promise_N).

Returns​

Promise<any>

A Promise linked to the queued item's resolution, or Promise.all for an array.


setConcurrency()​

setConcurrency(concurrency): void

Defined in: packages/utils/src/throttler.ts:126

Set the concurrency

Parameters​

concurrency​

number

newValue

If decreased, it will be in effect only when current promises resolve If increased, it will have immediate effect

Returns​

void


wait()​

wait(): Promise<void>

Defined in: packages/utils/src/throttler.ts:159

Wait until all queued and in-progress items have settled.

Returns​

Promise<void>

A Promise that resolves when the queue is empty, or rejects if failFast is enabled and any item failed.


run()​

static run(method, concurrency?): Promise<void>

Defined in: packages/utils/src/throttler.ts:72

Convenience method: create a one-shot Throttler, queue the given method(s), and wait for completion.

Parameters​

method​

(() => Promise<any>) | () => Promise<any>[]

A single executor function or an array of executor functions to queue.

concurrency?​

number = 10

Maximum concurrent executions (default 10).

Returns​

Promise<void>

A Promise that resolves when all queued items complete.