-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample.js
More file actions
19 lines (16 loc) · 722 Bytes
/
example.js
File metadata and controls
19 lines (16 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import Redis from 'ioredis'
import RateLimiter from './index.js'
const ratelimiter = RateLimiter({
client: new Redis(),
key: 'limiter',
limit: 3,
duration: 1000,
difference: 0, // allow no interval between requests
})
;(async () => {
await ratelimiter().then(console.log) // { total: 1, acknowledged: 1, remaining: 2 }
await ratelimiter().then(console.log) // { total: 2, acknowledged: 1, remaining: 1 }
await ratelimiter().then(console.log) // { total: 3, acknowledged: 1, remaining: 0 }
await ratelimiter().then(console.log).catch(console.error) // 429 - Error: Too Many Requests
await ratelimiter.get().then(console.log) // { total: 3, remaining: 0, retryAfterMS: 999 }
})().catch(console.error)