Defined in: async-debouncer.ts:169
A class that creates an async debounced function.
Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation. Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing or input changes where you only want to execute the handler after the events have stopped occurring.
Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until the function stops being called for the specified delay period.
Unlike the non-async Debouncer, this async version supports returning values from the debounced 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 debounced function.
Error Handling:
State Management:
const asyncDebouncer = new AsyncDebouncer(async (value: string) => {
const results = await searchAPI(value);
return results; // Return value is preserved
}, {
wait: 500,
onError: (error) => {
console.error('Search failed:', error);
}
});
// Called on each keystroke but only executes after 500ms of no typing
// Returns the API response directly
const results = await asyncDebouncer.maybeExecute(inputElement.value);
const asyncDebouncer = new AsyncDebouncer(async (value: string) => {
const results = await searchAPI(value);
return results; // Return value is preserved
}, {
wait: 500,
onError: (error) => {
console.error('Search failed:', error);
}
});
// Called on each keystroke but only executes after 500ms of no typing
// Returns the API response directly
const results = await asyncDebouncer.maybeExecute(inputElement.value);
• TFn extends AnyAsyncFunction
new AsyncDebouncer<TFn>(fn, initialOptions): AsyncDebouncer<TFn>
new AsyncDebouncer<TFn>(fn, initialOptions): AsyncDebouncer<TFn>
Defined in: async-debouncer.ts:180
TFn
AsyncDebouncer<TFn>
options: AsyncDebouncerOptions<TFn>;
options: AsyncDebouncerOptions<TFn>;
Defined in: async-debouncer.ts:173
readonly store: Store<Readonly<AsyncDebouncerState<TFn>>>;
readonly store: Store<Readonly<AsyncDebouncerState<TFn>>>;
Defined in: async-debouncer.ts:170
cancel(): void
cancel(): void
Defined in: async-debouncer.ts:363
Cancels any pending execution or aborts any execution in progress
void
flush(): void
flush(): void
Defined in: async-debouncer.ts:325
Processes the current pending execution immediately
void
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
Defined in: async-debouncer.ts:254
Attempts to execute the debounced function. If a call is already in progress, it will be queued.
Error Handling:
...Parameters<TFn>
Promise<undefined | ReturnType<TFn>>
A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError
The error from the debounced function if no onError handler is configured
reset(): void
reset(): void
Defined in: async-debouncer.ts:372
Resets the debouncer state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: async-debouncer.ts:195
Updates the async debouncer options
Partial<AsyncDebouncerOptions<TFn>>
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.