1.0.4 โข Published 6 months ago
@bytekit/throttle v1.0.4
@bytekit/throttle
A simple, type-safe, and efficient throttling utility for managing execution rate limits in JavaScript/TypeScript.
Features
- โ Supports both void and promise-returning tasks
- โณ Enforces throttling using token-bucket-style logic
- ๐ฅ Simple, performant, and dependency-free
- ๐งช 100% test and mutation coverage
- ๐ฅ Throws when queue is full
Installation
npm install @bytekit/throttleor
yarn add @bytekit/throttleUsage
ThrottledQueue
import {ThrottledQueue} from "@bytekit/throttle";
const queue = new ThrottledQueue(5, 1000); // 5 tasks per 1000ms
queue.enqueue(() => {
console.log("Task executed");
});ThrottledPromiseQueue
import {ThrottledPromiseQueue} from "@bytekit/throttle";
const queue = new ThrottledPromiseQueue(2, 1000);
const result = await queue.submit(async () => {
await new Promise((resolve) => setTimeout(resolve, 100));
return "Hello, throttling!";
});
console.log(result); // "Hello, throttling!"License
ISC