Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

p-throttle

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-throttle - npm Package Compare versions

Comparing version 6.0.0 to 6.1.0

32

index.d.ts

@@ -45,2 +45,34 @@ export class AbortError extends Error {

readonly strict?: boolean;
/**
Get notified when function calls are delayed due to exceeding the `limit` of allowed calls within the given `interval`.
Can be useful for monitoring the throttling efficiency.
@example
```
import pThrottle from 'p-throttle';
const throttle = pThrottle({
limit: 2,
interval: 1000,
onDelay: () => {
console.log('Reached interval limit, call is delayed');
},
});
const throttled = throttle(() => {
console.log('Executing...');
});
await throttled();
await throttled();
await throttled();
//=> Executing...
//=> Executing...
//=> Reached interval limit, call is delayed
//=> Executing...
```
*/
readonly onDelay?: () => void;
};

@@ -47,0 +79,0 @@

13

index.js

@@ -8,3 +8,3 @@ export class AbortError extends Error {

export default function pThrottle({limit, interval, strict}) {
export default function pThrottle({limit, interval, strict, onDelay}) {
if (!Number.isFinite(limit)) {

@@ -84,5 +84,10 @@ throw new TypeError('Expected `limit` to be a finite number');

timeoutId = setTimeout(execute, getDelay());
queue.set(timeoutId, reject);
const delay = getDelay();
if (delay > 0) {
timeoutId = setTimeout(execute, delay);
queue.set(timeoutId, reject);
onDelay?.();
} else {
execute();
}
});

@@ -89,0 +94,0 @@ };

{
"name": "p-throttle",
"version": "6.0.0",
"version": "6.1.0",
"description": "Throttle promise-returning & async functions",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -71,3 +71,3 @@ # p-throttle

#### strict
##### strict

@@ -79,2 +79,36 @@ Type: `boolean`\

##### onDelay
Type: `Function`
Get notified when function calls are delayed due to exceeding the `limit` of allowed calls within the given `interval`.
Can be useful for monitoring the throttling efficiency.
In the following example, the third call gets delayed and triggers the `onDelay` callback:
```js
import pThrottle from 'p-throttle';
const throttle = pThrottle({
limit: 2,
interval: 1000,
onDelay: () => {
console.log('Reached interval limit, call is delayed');
},
});
const throttled = throttle(() => {
console.log('Executing...');
});
await throttled();
await throttled();
await throttled();
//=> Executing...
//=> Executing...
//=> Reached interval limit, call is delayed
//=> Executing...
```
### throttle(function_)

@@ -81,0 +115,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc