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

asyncDebounce

Function: asyncDebounce()

ts
function asyncDebounce<TFn, TArgs>(fn, initialOptions): (...args) => Promise<void>
function asyncDebounce<TFn, TArgs>(fn, initialOptions): (...args) => Promise<void>

Defined in: async-debouncer.ts:219

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.

Type Parameters

TFn extends AnyAsyncFunction

TArgs extends any[]

Parameters

fn

TFn

initialOptions

Omit<AsyncDebouncerOptions<TFn, TArgs>, "enabled">

Returns

Function

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

Parameters

args

...TArgs

Returns

Promise<void>

Example

ts
const debounced = asyncDebounce(async (value: string) => {
  await saveToAPI(value);
}, { wait: 1000 });

// Will only execute once, 1 second after the last call
await debounced("first");  // Cancelled
await debounced("second"); // Cancelled
await debounced("third");  // Executes after 1s
const debounced = asyncDebounce(async (value: string) => {
  await saveToAPI(value);
}, { wait: 1000 });

// Will only execute once, 1 second after the last call
await debounced("first");  // Cancelled
await debounced("second"); // Cancelled
await debounced("third");  // Executes after 1s
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.