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

AsyncDebouncer

Class: AsyncDebouncer<TFn>

Defined in: async-debouncer.ts:101

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:

  • If an error occurs during execution and no onError handler is provided, the error will be thrown and propagate up to the caller.
  • If an onError handler is provided, errors will be caught and passed to the handler instead of being thrown.
  • The error count can be tracked using getErrorCount().
  • The debouncer maintains its state and can continue to be used after an error occurs.

Example

ts
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);

Type Parameters

TFn extends AnyAsyncFunction

Constructors

new AsyncDebouncer()

ts
new AsyncDebouncer<TFn>(fn, initialOptions): AsyncDebouncer<TFn>
new AsyncDebouncer<TFn>(fn, initialOptions): AsyncDebouncer<TFn>

Defined in: async-debouncer.ts:114

Parameters

fn

TFn

initialOptions

AsyncDebouncerOptions<TFn>

Returns

AsyncDebouncer<TFn>

Methods

cancel()

ts
cancel(): void
cancel(): void

Defined in: async-debouncer.ts:253

Cancels any pending execution or aborts any execution in progress

Returns

void


getEnabled()

ts
getEnabled(): boolean
getEnabled(): boolean

Defined in: async-debouncer.ts:148

Returns the current debouncer enabled state

Returns

boolean


getErrorCount()

ts
getErrorCount(): number
getErrorCount(): number

Defined in: async-debouncer.ts:282

Returns the number of times the function has errored

Returns

number


getIsExecuting()

ts
getIsExecuting(): boolean
getIsExecuting(): boolean

Defined in: async-debouncer.ts:296

Returns true if there is currently an execution in progress

Returns

boolean


getIsPending()

ts
getIsPending(): boolean
getIsPending(): boolean

Defined in: async-debouncer.ts:289

Returns true if there is a pending execution queued up for trailing execution

Returns

boolean


getLastResult()

ts
getLastResult(): undefined | ReturnType<TFn>
getLastResult(): undefined | ReturnType<TFn>

Defined in: async-debouncer.ts:261

Returns the last result of the debounced function

Returns

undefined | ReturnType<TFn>


getOptions()

ts
getOptions(): AsyncDebouncerOptions<TFn>
getOptions(): AsyncDebouncerOptions<TFn>

Defined in: async-debouncer.ts:141

Returns the current debouncer options

Returns

AsyncDebouncerOptions<TFn>


getSettleCount()

ts
getSettleCount(): number
getSettleCount(): number

Defined in: async-debouncer.ts:275

Returns the number of times the function has settled (completed or errored)

Returns

number


getSuccessCount()

ts
getSuccessCount(): number
getSuccessCount(): number

Defined in: async-debouncer.ts:268

Returns the number of times the function has been executed successfully

Returns

number


getWait()

ts
getWait(): number
getWait(): number

Defined in: async-debouncer.ts:155

Returns the current debouncer wait state

Returns

number


maybeExecute()

ts
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>

Defined in: async-debouncer.ts:173

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


setOptions()

ts
setOptions(newOptions): void
setOptions(newOptions): void

Defined in: async-debouncer.ts:129

Updates the debouncer options Returns the new options state

Parameters

newOptions

Partial<AsyncDebouncerOptions<TFn>>

Returns

void

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.