Skip to content

rate ​

The rate function limits the number of times a function can be executed within a given time interval, useful for rate limiting frequent calls.

Syntax ​

typescript
rate(callback, limit, interval)

Parameters ​

NameTypeDescription
callbackFunctionFunction to be executed.
limitnumberMaximum number of executions allowed per interval.
intervalnumberTime interval in milliseconds for the limit.

Return Value ​

TypeDescription
FunctionExecutes the callback if the limit has not been reached and returns true, else returns false

Examples ​

typescript
const limited = rate(() => console.log('Executed!'), 2, 1000);

limited(); // true
limited(); // true
limited(); // false (limit reached)
// After 1 second, the limit is reset

Notes ​

  • Throws a TypeError if the callback is not a function or if limit/interval are not positive numbers.
  • Useful for preventing resource overload or excessive API calls.

References ​

Released under the MIT License.