Defined in: async-throttler.ts:59
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.
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);
});
• TFn extends AnyAsyncFunction
• TArgs extends Parameters<TFn>
new AsyncThrottler<TFn, TArgs>(fn, initialOptions): AsyncThrottler<TFn, TArgs>
new AsyncThrottler<TFn, TArgs>(fn, initialOptions): AsyncThrottler<TFn, TArgs>
Defined in: async-throttler.ts:72
TFn
AsyncThrottlerOptions<TFn, TArgs>
AsyncThrottler<TFn, TArgs>
cancel(): void
cancel(): void
Defined in: async-throttler.ts:169
Cancels any pending execution
void
getExecutionCount(): number
getExecutionCount(): number
Defined in: async-throttler.ts:181
Returns the number of times the function has been executed
number
getIsPending(): boolean
getIsPending(): boolean
Defined in: async-throttler.ts:202
Returns the current pending state
boolean
getLastExecutionTime(): number
getLastExecutionTime(): number
Defined in: async-throttler.ts:188
Returns the last execution time
number
getNextExecutionTime(): number
getNextExecutionTime(): number
Defined in: async-throttler.ts:195
Returns the next execution time
number
getOptions(): Required<AsyncThrottlerOptions<TFn, TArgs>>
getOptions(): Required<AsyncThrottlerOptions<TFn, TArgs>>
Defined in: async-throttler.ts:99
Returns the current options
Required<AsyncThrottlerOptions<TFn, TArgs>>
maybeExecute(...args): Promise<void>
maybeExecute(...args): Promise<void>
Defined in: async-throttler.ts:107
Attempts to execute the throttled function If a call is already in progress, it may be blocked or queued depending on the wait option
...TArgs
Promise<void>
setOptions(newOptions): Required<AsyncThrottlerOptions<TFn, TArgs>>
setOptions(newOptions): Required<AsyncThrottlerOptions<TFn, TArgs>>
Defined in: async-throttler.ts:86
Updates the throttler options Returns the new options state
Partial<AsyncThrottlerOptions<TFn, TArgs>>
Required<AsyncThrottlerOptions<TFn, TArgs>>
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.