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:285
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.
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.
• TFn extends AnyAsyncFunction
TFn
Function
Attempts to execute the debounced function If a call is already in progress, it will be queued
...Parameters<TFn>
Promise<undefined | ReturnType<TFn>>
const debounced = asyncDebounce(async (value: string) => {
const result = await saveToAPI(value);
return result; // Return value is preserved
}, { wait: 1000 });
// 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 });
// 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.