https://github.com/sindresorhus/p-debounce
Debounce promise-returning & async functions
https://github.com/sindresorhus/p-debounce
Last synced: 8 months ago
JSON representation
Debounce promise-returning & async functions
- Host: GitHub
- URL: https://github.com/sindresorhus/p-debounce
- Owner: sindresorhus
- License: mit
- Created: 2016-10-21T08:01:47.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2024-04-14T16:38:57.000Z (over 1 year ago)
- Last Synced: 2025-04-03T15:09:17.140Z (8 months ago)
- Language: JavaScript
- Size: 25.4 KB
- Stars: 216
- Watchers: 4
- Forks: 24
- Open Issues: 4
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- promise-fun - p-debounce - returning & async functions (Packages)
- awesome-promises - p-debounce - Debounce promise-returning & async functions (Convenience Utilities / sindresorhus's many Promise utilities ([see notes](https://github.com/sindresorhus/promise-fun)))
- fucking-awesome-promises - p-debounce - Debounce promise-returning & async functions (Convenience Utilities / sindresorhus's many Promise utilities (<b><code> 5095⭐</code></b> <b><code> 138🍴</code></b> [see notes](https://github.com/sindresorhus/promise-fun))))
README
# p-debounce
> [Debounce](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions
## Install
```sh
npm install p-debounce
```
## Usage
```js
import pDebounce from 'p-debounce';
const expensiveCall = async input => input;
const debouncedFunction = pDebounce(expensiveCall, 200);
for (const number of [1, 2, 3]) {
(async () => {
console.log(await debouncedFunction(number));
})();
}
//=> 3
//=> 3
//=> 3
```
## API
### pDebounce(fn, wait, options?)
Returns a function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called.
#### fn
Type: `Function`
Promise-returning/async function to debounce.
#### wait
Type: `number`
Milliseconds to wait before calling `fn`.
#### options
Type: `object`
##### before
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.
### pDebounce.promise(function_)
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.
#### function_
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 debouncedFunction = pDebounce.promise(expensiveCall);
for (const number of [1, 2, 3]) {
(async () => {
console.log(await debouncedFunction(number));
})();
}
//=> 1
//=> 1
//=> 1
```
## Related
- [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)