p-debounce
Version:
Debounce promise-returning & async functions
93 lines (59 loc) • 2.16 kB
Markdown
> [Debounce](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions
```
$ npm install p-debounce
```
```js
import pDebounce from 'p-debounce';
const expensiveCall = async input => input;
const debouncedFn = pDebounce(expensiveCall, 200);
for (const number of [1, 2, 3]) {
console.log(await debouncedFn(number));
}
//=> 3
//=> 3
//=> 3
```
Returns a function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called.
Type: `Function`
Promise-returning/async function to debounce.
Type: `number`
Milliseconds to wait before calling `fn`.
Type: `object`
Type: `boolean`\
Default: `false`
Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1). Meaning immediately, instead of waiting for `wait` milliseconds.
Execute `function_` unless a previous call is still pending, in which case, return the pending promise. Useful, for example, to avoid processing extra button clicks if the previous one is not complete.
Type: `Function`
Promise-returning/async function to debounce.
```js
import {setTimeout as delay} from 'timers/promises';
import pDebounce from 'p-debounce';
const expensiveCall = async value => {
await delay(200);
return value;
}
const debouncedFn = pDebounce.promise(expensiveCall);
for (const number of [1, 2, 3]) {
console.log(await debouncedFn(number));
}
//=> 1
//=> 2
//=> 3
```
- [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
- [p-limit](https://github.com/sindresorhus/p-limit) - Run multiple promise-returning & async functions with limited concurrency
- [p-memoize](https://github.com/sindresorhus/p-memoize) - Memoize promise-returning & async functions
- [debounce-fn](https://github.com/sindresorhus/debounce-fn) - Debounce a function
- [More…](https://github.com/sindresorhus/promise-fun)