What is p-debounce?
The p-debounce npm package is a utility for debouncing promise-returning and async functions. Debouncing is a technique used to limit the rate at which a function can fire. It ensures that the function is only called once after a specified delay has elapsed since the last time it was invoked.
What are p-debounce's main functionalities?
Basic Debouncing
This feature allows you to debounce an async function. The function will only be called once after the specified delay (200ms in this case) has elapsed since the last invocation.
const pDebounce = require('p-debounce');
const expensiveFunction = async input => {
console.log('Processing:', input);
return input;
};
const debouncedFunction = pDebounce(expensiveFunction, 200);
debouncedFunction('first call');
debouncedFunction('second call');
// Only 'second call' will be processed after 200ms
Debouncing with Leading Edge
This feature allows you to debounce an async function with the option to trigger the function on the leading edge of the delay period. This means the function will be called immediately on the first invocation and then debounced for subsequent calls.
const pDebounce = require('p-debounce');
const expensiveFunction = async input => {
console.log('Processing:', input);
return input;
};
const debouncedFunction = pDebounce(expensiveFunction, 200, {leading: true});
debouncedFunction('first call');
debouncedFunction('second call');
// 'first call' will be processed immediately, 'second call' will be ignored if it occurs within 200ms
Other packages similar to p-debounce
lodash.debounce
lodash.debounce is a popular utility from the Lodash library that provides similar debouncing functionality. It is widely used and well-documented, but it does not natively support promise-returning or async functions like p-debounce does.
debounce
debounce is a lightweight npm package that provides basic debouncing functionality. It is simple and easy to use but lacks the advanced features and async support that p-debounce offers.
throttle-debounce
throttle-debounce is a package that provides both throttling and debouncing utilities. It is versatile and can be used for various rate-limiting scenarios, but it does not specifically cater to promise-returning or async functions like p-debounce.
p-debounce 
Debounce promise-returning & async functions
Install
$ npm install --save p-debounce
Usage
const pDebounce = require('p-debounce');
const expensiveCall = input => Promise.resolve(input);
const debouncedFn = pDebounce(expensiveCall, 200);
for (const i of [1, 2, 3]) {
debouncedFn(i).then(console.log);
}
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.
wait
Type: number
Milliseconds to wait before calling fn
.
options
Type: Object
leading
Type: boolean
Default: false
Call the fn
on the leading edge of the timeout. Meaning immediately, instead of waiting for wait
milliseconds.
Related
- p-throttle - Throttle promise-returning & async functions
- p-limit - Run multiple promise-returning & async functions with limited concurrency
- p-memoize - Memoize promise-returning & async functions
- More…
License
MIT © Sindre Sorhus