Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dsnsgithub/simple-call-limiter
A simple function rate limiter that delays execution of functions based on a specified delay.
https://github.com/dsnsgithub/simple-call-limiter
delay npm ratelimit
Last synced: 5 days ago
JSON representation
A simple function rate limiter that delays execution of functions based on a specified delay.
- Host: GitHub
- URL: https://github.com/dsnsgithub/simple-call-limiter
- Owner: dsnsgithub
- License: lgpl-3.0
- Created: 2024-02-19T18:30:39.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-02-26T06:23:47.000Z (9 months ago)
- Last Synced: 2024-11-03T16:51:48.025Z (11 days ago)
- Topics: delay, npm, ratelimit
- Language: TypeScript
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple Call Limiter
A simple function rate limiter that delays execution of functions based on a specified delay.
## Installation
You can install [simple-call-limiter](https://www.npmjs.com/package/simple-call-limiter) via npm:
```bash
npm install simple-call-limiter
```## API
### SimpleLimiter(delayMs: number)
Creates a new instance of SimpleDelay with the specified delay in milliseconds.
- `delayMs`: The delay in milliseconds between function calls.
### run(fn, ...args)
Runs the provided function (with arguments) after ensuring the specified delay. If multiple calls are made within the delay period, they are queued and executed sequentially.
- `fn`: The function to be executed.
- `args`: Arguments to be passed to the function.Returns a Promise that resolves when the function is executed. If you are using Typescript, the arguments are checked and the output is a promise with the output type of the function.
## Typescript Example
```typescript
import SimpleLimiter from "simple-call-limiter";// Create a new instance of SimpleLimiter with a delay of 1000 milliseconds
const limiter = new SimpleLimiter(1000);for (let i = 0; i < 5; i++) {
limiter.run(console.log, i);
}// output
// 0 (immediate)
// 1 (after 1 sec)
// 2 (after 2 sec)
// 3 (after 3 sec)
// 4 (after 4 sec)
```## JavaScript Example
```javascript
const SimpleLimiter = require('simple-call-limiter').default;// Create a new instance of SimpleLimiter with a delay of 1000 milliseconds
const limiter = new SimpleLimiter(1000);for (let i = 0; i < 5; i++) {
limiter.run(console.log, i);
}
```## Typescript Promise Example
```typescript
import SimpleLimiter from "simple-call-limiter";// Create a new instance of SimpleLimiter with a delay of 1000 milliseconds
const limiter = new SimpleLimiter(1000);async function asyncTest(item: string) {
return item;
}async function run() {
for (let i = 0; i < 5; i++) {
const result = await limiter.run(asyncTest, "testing");
const result2 = await limiter.run(asyncTest) //! this will be flagged since asyncTest takes an argument and none is given.console.log(typeof result); // type information is kept (string)
console.log(result);
}
}run();
```