Skip to content

webshrine / stdlib/src / debounce

Function: debounce()

debounce<ArgumentsT, ReturnT>(fn, wait?, options?): (...args) => Promise<ReturnT>

Type Parameters

ArgumentsT extends unknown[]

ReturnT

Parameters

fn

(...args) => ReturnT | PromiseLike<ReturnT>

Promise-returning/async function to debounce.

wait?

number

Milliseconds to wait before calling fn. Default value is 25ms

options?

DebounceOptions

Returns

Function

A function that delays calling fn until after wait milliseconds have elapsed since the last time it was called.

Parameters

args

...ArgumentsT

Returns

Promise<ReturnT>

Example

import { debounce } from 'perfect-debounce';
const expensiveCall = async input => input;
const debouncedFn = debounce(expensiveCall, 200);
for (const number of [1, 2, 3]) {
  console.log(await debouncedFn(number));
}
//=> 3
//=> 3
//=> 3

Defined in

packages/stdlib/src/wrappers/index.ts:10