Defined in: async-throttler.ts:80
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.
Unlike the non-async Throttler, this async version supports returning values from the throttled function, making it ideal for API calls and other async operations where you want the result of the maybeExecute call instead of setting the result on a state variable from within the throttled function.
This is useful for rate-limiting API calls, handling scroll/resize events, or any scenario where you want to ensure a maximum execution frequency.
const throttler = new AsyncThrottler(async (value: string) => {
const result = await saveToAPI(value);
return result; // Return value is preserved
}, { wait: 1000 });
// Will only execute once per second no matter how often called
// Returns the API response directly
const result = await throttler.maybeExecute(inputElement.value);
const throttler = new AsyncThrottler(async (value: string) => {
const result = await saveToAPI(value);
return result; // Return value is preserved
}, { wait: 1000 });
// Will only execute once per second no matter how often called
// Returns the API response directly
const result = await throttler.maybeExecute(inputElement.value);
• TFn extends AnyAsyncFunction
new AsyncThrottler<TFn>(fn, initialOptions): AsyncThrottler<TFn>
new AsyncThrottler<TFn>(fn, initialOptions): AsyncThrottler<TFn>
Defined in: async-throttler.ts:93
TFn
AsyncThrottler<TFn>
cancel(): void
cancel(): void
Defined in: async-throttler.ts:191
Cancels any pending execution or aborts any execution in progress
void
getErrorCount(): number
getErrorCount(): number
Defined in: async-throttler.ts:241
Returns the number of times the function has errored
number
getIsExecuting(): boolean
getIsExecuting(): boolean
Defined in: async-throttler.ts:255
Returns the current executing state
boolean
getIsPending(): boolean
getIsPending(): boolean
Defined in: async-throttler.ts:248
Returns the current pending state
boolean
getLastExecutionTime(): number
getLastExecutionTime(): number
Defined in: async-throttler.ts:206
Returns the last execution time
number
getLastResult(): undefined | ReturnType<TFn>
getLastResult(): undefined | ReturnType<TFn>
Defined in: async-throttler.ts:220
Returns the last result of the debounced function
undefined | ReturnType<TFn>
getNextExecutionTime(): number
getNextExecutionTime(): number
Defined in: async-throttler.ts:213
Returns the next execution time
number
getOptions(): Required<AsyncThrottlerOptions<TFn>>
getOptions(): Required<AsyncThrottlerOptions<TFn>>
Defined in: async-throttler.ts:119
Returns the current options
Required<AsyncThrottlerOptions<TFn>>
getSettleCount(): number
getSettleCount(): number
Defined in: async-throttler.ts:234
Returns the number of times the function has settled (completed or errored)
number
getSuccessCount(): number
getSuccessCount(): number
Defined in: async-throttler.ts:227
Returns the number of times the function has been executed successfully
number
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
Defined in: async-throttler.ts:127
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<TFn>
Promise<undefined | ReturnType<TFn>>
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: async-throttler.ts:107
Updates the throttler options Returns the new options state
Partial<AsyncThrottlerOptions<TFn>>
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.