Defined in: async-throttler.ts:230
A class that creates an async throttled function.
Async vs Sync Versions: The async version provides advanced features over the sync Throttler:
The sync Throttler is lighter weight and simpler when you don't need async features, return values, or execution control.
What is Throttling? 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.
Error Handling:
State Management:
const throttler = new AsyncThrottler(async (value: string) => {
const result = await saveToAPI(value);
return result; // Return value is preserved
}, {
wait: 1000,
onError: (error) => {
console.error('API call failed:', error);
}
});
// 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,
onError: (error) => {
console.error('API call failed:', error);
}
});
// 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:242
TFn
AsyncThrottler<TFn>
asyncRetryers: Map<number, AsyncRetryer<TFn>>;
asyncRetryers: Map<number, AsyncRetryer<TFn>>;
Defined in: async-throttler.ts:236
fn: TFn;
fn: TFn;
Defined in: async-throttler.ts:243
key: string;
key: string;
Defined in: async-throttler.ts:234
options: AsyncThrottlerOptions<TFn>;
options: AsyncThrottlerOptions<TFn>;
Defined in: async-throttler.ts:235
readonly store: Store<Readonly<AsyncThrottlerState<TFn>>>;
readonly store: Store<Readonly<AsyncThrottlerState<TFn>>>;
Defined in: async-throttler.ts:231
_emit(): void
_emit(): void
Defined in: async-throttler.ts:264
Emits a change event for the async throttler instance. Mostly useful for devtools.
void
abort(): void
abort(): void
Defined in: async-throttler.ts:532
Aborts all ongoing executions with the internal abort controllers. Does NOT cancel any pending execution that have not started yet.
void
cancel(): void
cancel(): void
Defined in: async-throttler.ts:542
Cancels any pending execution that have not started yet. Does NOT abort any execution already in progress.
void
flush(): Promise<undefined | ReturnType<TFn>>
flush(): Promise<undefined | ReturnType<TFn>>
Defined in: async-throttler.ts:461
Processes the current pending execution immediately
Promise<undefined | ReturnType<TFn>>
getAbortSignal(maybeExecuteCount?): null | AbortSignal
getAbortSignal(maybeExecuteCount?): null | AbortSignal
Defined in: async-throttler.ts:522
Returns the AbortSignal for a specific execution. If no maybeExecuteCount is provided, returns the signal for the most recent execution. Returns null if no execution is found or not currently executing.
number
Optional specific execution to get signal for
null | AbortSignal
const throttler = new AsyncThrottler(
async (data: string) => {
const signal = throttler.getAbortSignal()
if (signal) {
const response = await fetch('/api/save', {
method: 'POST',
body: data,
signal
})
return response.json()
}
},
{ wait: 1000 }
)
const throttler = new AsyncThrottler(
async (data: string) => {
const signal = throttler.getAbortSignal()
if (signal) {
const response = await fetch('/api/save', {
method: 'POST',
body: data,
signal
})
return response.json()
}
},
{ wait: 1000 }
)
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
Defined in: async-throttler.ts:337
Attempts to execute the throttled function. The execution behavior depends on the throttler options:
If enough time has passed since the last execution (>= wait period):
If within the wait period:
...Parameters<TFn>
Promise<undefined | ReturnType<TFn>>
const throttled = new AsyncThrottler(fn, { wait: 1000 });
// First call executes immediately
await throttled.maybeExecute('a', 'b');
// Call during wait period - gets throttled
await throttled.maybeExecute('c', 'd');
const throttled = new AsyncThrottler(fn, { wait: 1000 });
// First call executes immediately
await throttled.maybeExecute('a', 'b');
// Call during wait period - gets throttled
await throttled.maybeExecute('c', 'd');
reset(): void
reset(): void
Defined in: async-throttler.ts:556
Resets the debouncer state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: async-throttler.ts:269
Updates the async throttler options
Partial<AsyncThrottlerOptions<TFn>>
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.