Framework
Version
防抖器 API 參考
節流器 API 參考
速率限制器 API 參考
佇列 API 參考

AsyncThrottler

Class: AsyncThrottler<TFn>

Defined in: async-throttler.ts:76

A class that creates an async throttled function.

Throttling limits how often a function can be executed, allowing only one execution within a specified time window. Unlike debouncing which resets the delay timer on each call, throttling ensures the function executes at a regular interval regardless of how often it's called.

This is useful for rate-limiting API calls, handling scroll/resize events, or any scenario where you want to ensure a maximum execution frequency.

Example

ts
const throttler = new AsyncThrottler(async (value: string) => {
  await saveToAPI(value);
}, { wait: 1000 });

// Will only execute once per second no matter how often called
inputElement.addEventListener('input', () => {
  throttler.maybeExecute(inputElement.value);
});
const throttler = new AsyncThrottler(async (value: string) => {
  await saveToAPI(value);
}, { wait: 1000 });

// Will only execute once per second no matter how often called
inputElement.addEventListener('input', () => {
  throttler.maybeExecute(inputElement.value);
});

Type Parameters

TFn extends AnyAsyncFunction

Constructors

new AsyncThrottler()

ts
new AsyncThrottler<TFn>(fn, initialOptions): AsyncThrottler<TFn>
new AsyncThrottler<TFn>(fn, initialOptions): AsyncThrottler<TFn>

Defined in: async-throttler.ts:89

Parameters

fn

TFn

initialOptions

AsyncThrottlerOptions<TFn>

Returns

AsyncThrottler<TFn>

Methods

cancel()

ts
cancel(): void
cancel(): void

Defined in: async-throttler.ts:187

Cancels any pending execution or aborts any execution in progress

Returns

void


getErrorCount()

ts
getErrorCount(): number
getErrorCount(): number

Defined in: async-throttler.ts:237

Returns the number of times the function has errored

Returns

number


getIsExecuting()

ts
getIsExecuting(): boolean
getIsExecuting(): boolean

Defined in: async-throttler.ts:251

Returns the current executing state

Returns

boolean


getIsPending()

ts
getIsPending(): boolean
getIsPending(): boolean

Defined in: async-throttler.ts:244

Returns the current pending state

Returns

boolean


getLastExecutionTime()

ts
getLastExecutionTime(): number
getLastExecutionTime(): number

Defined in: async-throttler.ts:202

Returns the last execution time

Returns

number


getLastResult()

ts
getLastResult(): undefined | ReturnType<TFn>
getLastResult(): undefined | ReturnType<TFn>

Defined in: async-throttler.ts:216

Returns the last result of the debounced function

Returns

undefined | ReturnType<TFn>


getNextExecutionTime()

ts
getNextExecutionTime(): number
getNextExecutionTime(): number

Defined in: async-throttler.ts:209

Returns the next execution time

Returns

number


getOptions()

ts
getOptions(): Required<AsyncThrottlerOptions<TFn>>
getOptions(): Required<AsyncThrottlerOptions<TFn>>

Defined in: async-throttler.ts:115

Returns the current options

Returns

Required<AsyncThrottlerOptions<TFn>>


getSettleCount()

ts
getSettleCount(): number
getSettleCount(): number

Defined in: async-throttler.ts:230

Returns the number of times the function has settled (completed or errored)

Returns

number


getSuccessCount()

ts
getSuccessCount(): number
getSuccessCount(): number

Defined in: async-throttler.ts:223

Returns the number of times the function has been executed successfully

Returns

number


maybeExecute()

ts
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>

Defined in: async-throttler.ts:123

Attempts to execute the throttled function If a call is already in progress, it may be blocked or queued depending on the wait option

Parameters

args

...Parameters<TFn>

Returns

Promise<undefined | ReturnType<TFn>>


setOptions()

ts
setOptions(newOptions): void
setOptions(newOptions): void

Defined in: async-throttler.ts:103

Updates the throttler options Returns the new options state

Parameters

newOptions

Partial<AsyncThrottlerOptions<TFn>>

Returns

void

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.