Framework
Version
防抖器 API 參考
節流器 API 參考
速率限制器 API 參考
佇列 API 參考

AsyncDebouncer

Class: AsyncDebouncer<TFn>

Defined in: async-debouncer.ts:73

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.

Example

ts
const asyncDebouncer = new AsyncDebouncer(async (value: string) => {
  await searchAPI(value);
}, { wait: 500 });

// Called on each keystroke but only executes after 500ms of no typing
inputElement.addEventListener('input', () => {
  asyncDebouncer.maybeExecute(inputElement.value);
});
const asyncDebouncer = new AsyncDebouncer(async (value: string) => {
  await searchAPI(value);
}, { wait: 500 });

// Called on each keystroke but only executes after 500ms of no typing
inputElement.addEventListener('input', () => {
  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:86

Parameters

fn

TFn

initialOptions

AsyncDebouncerOptions<TFn>

Returns

AsyncDebouncer<TFn>

Methods

cancel()

ts
cancel(): void
cancel(): void

Defined in: async-debouncer.ts:195

Cancels any pending execution or aborts any execution in progress

Returns

void


getErrorCount()

ts
getErrorCount(): number
getErrorCount(): number

Defined in: async-debouncer.ts:224

Returns the number of times the function has errored

Returns

number


getIsExecuting()

ts
getIsExecuting(): boolean
getIsExecuting(): boolean

Defined in: async-debouncer.ts:238

Returns true if there is currently an execution in progress

Returns

boolean


getIsPending()

ts
getIsPending(): boolean
getIsPending(): boolean

Defined in: async-debouncer.ts:231

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:203

Returns the last result of the debounced function

Returns

undefined | ReturnType<TFn>


getOptions()

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

Defined in: async-debouncer.ts:112

Returns the current debouncer options

Returns

Required<AsyncDebouncerOptions<TFn>>


getSettleCount()

ts
getSettleCount(): number
getSettleCount(): number

Defined in: async-debouncer.ts:217

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:210

Returns the number of times the function has been executed successfully

Returns

number


maybeExecute()

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

Defined in: async-debouncer.ts:120

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

Parameters

args

...Parameters<TFn>

Returns

Promise<undefined | ReturnType<TFn>>


setOptions()

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

Defined in: async-debouncer.ts:100

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.