Framework
Version
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference

asyncDebounce

Function: asyncDebounce()

ts
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:335

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.

Error Handling:

  • If an onError handler is provided, it will be called with the error and debouncer instance
  • If throwOnError is true (default when no onError handler is provided), the error will be thrown
  • If throwOnError is false (default when onError handler is provided), the error will be swallowed
  • The error state can be checked using the underlying AsyncDebouncer instance
  • Both onError and throwOnError can be used together - the handler will be called before any error is thrown

Type Parameters

TFn extends AnyAsyncFunction

Parameters

fn

TFn

initialOptions

AsyncDebouncerOptions<TFn>

Returns

Function

Attempts to execute the debounced function. If a call is already in progress, it will be queued.

Error Handling:

  • If the debounced function throws and no onError handler is configured, the error will be thrown from this method.
  • If an onError handler is configured, errors will be caught and passed to the handler, and this method will return undefined.
  • The error state can be checked using getErrorCount() and getIsExecuting().

Parameters

args

...Parameters<TFn>

Returns

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

Throws

The error from the debounced function if no onError handler is configured

Example

ts
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");
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.