function asyncDebounce<TFn>(fn, initialOptions): (...args) => Promise<undefined | ReturnType<TFn>>
function asyncDebounce<TFn>(fn, initialOptions): (...args) => Promise<undefined | ReturnType<TFn>>
Defined in: async-debouncer.ts:558
Creates an async debounced function that delays execution until after a specified wait time. The debounced function will only execute once the wait period has elapsed without any new calls. If called again during the wait period, the timer resets and a new wait period begins.
Async vs Sync Versions: The async version provides advanced features over the sync debounce function:
The sync debounce function is lighter weight and simpler when you don't need async features, return values, or execution control.
What is Debouncing? 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.
Configuration Options:
Error Handling:
State Management:
• TFn extends AnyAsyncFunction
TFn
Function
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
const debounced = asyncDebounce(async (value: string) => {
const result = await saveToAPI(value);
return result; // Return value is preserved
}, {
wait: 1000,
onError: (error) => {
console.error('API call failed:', error);
},
throwOnError: true // Will both log the error and throw it
});
// Will only execute once, 1 second after the last call
// Returns the API response directly
const result = await debounced("third");
const debounced = asyncDebounce(async (value: string) => {
const result = await saveToAPI(value);
return result; // Return value is preserved
}, {
wait: 1000,
onError: (error) => {
console.error('API call failed:', error);
},
throwOnError: true // Will both log the error and throw it
});
// Will only execute once, 1 second after the last call
// Returns the API response directly
const result = await debounced("third");
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
