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 5.1.0 to 6.0.0

16

index.d.ts

@@ -7,7 +7,5 @@ export class AbortError extends Error {

// TODO: Use the `Awaited` utility instead when targeting TS 4.5.
type PromiseResolve<ValueType> = ValueType extends PromiseLike<infer ValueType> ? Promise<ValueType> : Promise<ValueType>;
type AnyFunction = (...arguments_: readonly any[]) => unknown;
export type ThrottledFunction<Argument extends readonly unknown[], ReturnValue> = ((...arguments: Argument) => PromiseResolve<ReturnValue>)
& {
export type ThrottledFunction<F extends AnyFunction> = F & {
/**

@@ -31,3 +29,3 @@ Whether future function calls should be throttled or count towards throttling thresholds.

export interface Options {
export type Options = {
/**

@@ -49,7 +47,9 @@ The maximum number of calls within an `interval`.

readonly strict?: boolean;
}
};
/**
[Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning/async/normal functions.
Throttle promise-returning/async/normal functions.
It rate-limits function calls without discarding them, making it ideal for external API interactions where avoiding call loss is crucial.
@returns A throttle function.

@@ -88,2 +88,2 @@

*/
export default function pThrottle(options: Options): <Argument extends readonly unknown[], ReturnValue>(function_: (...arguments: Argument) => ReturnValue) => ThrottledFunction<Argument, ReturnValue>;
export default function pThrottle(options: Options): <F extends AnyFunction>(function_: F) => ThrottledFunction<F>;

@@ -46,2 +46,8 @@ export class AbortError extends Error {

// Clear the queue if there's a significant delay since the last execution
if (strictTicks.length > 0 && now - strictTicks.at(-1) > interval) {
strictTicks.length = 0;
}
// If the queue is not full, add the current time and execute immediately
if (strictTicks.length < limit) {

@@ -52,11 +58,11 @@ strictTicks.push(now);

const earliestTime = strictTicks.shift() + interval;
// Calculate the next execution time based on the first item in the queue
const nextExecutionTime = strictTicks[0] + interval;
if (now >= earliestTime) {
strictTicks.push(now);
return 0;
}
// Shift the queue and add the new execution time
strictTicks.shift();
strictTicks.push(nextExecutionTime);
strictTicks.push(earliestTime);
return earliestTime - now;
// Calculate the delay for the current execution
return Math.max(0, nextExecutionTime - now);
}

@@ -67,17 +73,17 @@

return function_ => {
const throttled = function (...args) {
const throttled = function (...arguments_) {
if (!throttled.isEnabled) {
return (async () => function_.apply(this, args))();
return (async () => function_.apply(this, arguments_))();
}
let timeout;
let timeoutId;
return new Promise((resolve, reject) => {
const execute = () => {
resolve(function_.apply(this, args));
queue.delete(timeout);
resolve(function_.apply(this, arguments_));
queue.delete(timeoutId);
};
timeout = setTimeout(execute, getDelay());
timeoutId = setTimeout(execute, getDelay());
queue.set(timeout, reject);
queue.set(timeoutId, reject);
});

@@ -84,0 +90,0 @@ };

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

@@ -14,5 +14,9 @@ "license": "MIT",

"type": "module",
"exports": "./index.js",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=18"
},

@@ -47,9 +51,12 @@ "scripts": {

"devDependencies": {
"ava": "^3.15.0",
"delay": "^5.0.0",
"ava": "^5.3.1",
"delay": "^6.0.0",
"in-range": "^3.0.0",
"time-span": "^5.0.0",
"tsd": "^0.17.0",
"xo": "^0.45.0"
"time-span": "^5.1.0",
"tsd": "^0.29.0",
"xo": "^0.56.0"
},
"ava": {
"serial": true
}
}
# p-throttle
> [Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions
> Throttle promise-returning & async functions
It also works with normal functions.
Useful for rate limiting calls to an external API, for example.
It rate-limits function calls without discarding them, making it ideal for external API interactions where avoiding call loss is crucial.

@@ -9,0 +9,0 @@ ## Install

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