
Company News
Socket Has Acquired Secure Annex
Socket has acquired Secure Annex to expand extension security across browsers, IDEs, and AI tools.
Debounce promise-returning & async functions
npm install p-debounce
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
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. Meaning immediately, instead of waiting for wait milliseconds.
Type: AbortSignal
An AbortSignal to cancel the debounced 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.
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
Type: Function
Promise-returning/async function to debounce.
Type: object
Type: boolean
Default: false
If a call is made while a previous call is still running, queue the latest arguments and run the function again after the current execution completes.
Use cases:
after: false (default): API fetches, data loading, read operations - concurrent calls share the same result.after: true: Saving data, file writes, state updates - ensures latest data is never lost.import {setTimeout as delay} from 'timers/promises';
import pDebounce from 'p-debounce';
const save = async data => {
await delay(200);
console.log(`Saved: ${data}`);
return data;
};
const debouncedSave = pDebounce.promise(save, {after: true});
// If data changes while saving, it will save again with the latest data
debouncedSave('data1');
debouncedSave('data2'); // This will run after the first save completes
//=> Saved: data1
//=> Saved: data2
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 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 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.
FAQs
Debounce promise-returning & async functions
The npm package p-debounce receives a total of 344,538 weekly downloads. As such, p-debounce popularity was classified as popular.
We found that p-debounce demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket has acquired Secure Annex to expand extension security across browsers, IDEs, and AI tools.

Research
/Security News
Socket is tracking cloned Open VSX extensions tied to GlassWorm, with several updated from benign-looking sleepers into malware delivery vehicles.

Product
Reachability analysis for PHP is now available in experimental, helping teams identify which vulnerabilities are actually exploitable.