![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
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.
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
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.
Debounce promise-returning & async functions
$ npm install p-debounce
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. 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.
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
FAQs
Debounce promise-returning & async functions
We found that p-debounce demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.