p-throttle
Advanced tools
Comparing version 6.0.0 to 6.1.0
@@ -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 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10483
183
145