https://github.com/takker99/deno-async-throttle
Throttle implementation for Deno/Web browsers.
https://github.com/takker99/deno-async-throttle
deno throttle
Last synced: 2 months ago
JSON representation
Throttle implementation for Deno/Web browsers.
- Host: GitHub
- URL: https://github.com/takker99/deno-async-throttle
- Owner: takker99
- License: mit
- Created: 2021-09-04T07:40:35.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-12-24T07:39:43.000Z (over 1 year ago)
- Last Synced: 2025-01-17T12:16:47.336Z (over 1 year ago)
- Topics: deno, throttle
- Language: TypeScript
- Homepage: https://jsr.io/@takker/throttle
- Size: 42 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @takker/throttle
[](https://jsr.io/@takker/throttle)
[](https://github.com/takker99/deno-async-throttle/actions?query=workflow%3Aci)
[Throttle](https://developer.mozilla.org/docs/Glossary/Throttle) implementation
for Deno/Web browsers.
## Usage
```ts
import { assertEquals, assertRejects } from "@std/assert";
import { throttle, type ThrottledState } from "@takker/throttle";
const throttledFn = throttle((i: number, state: ThrottledState) => {
if (i === 1 && state !== "discarded") throw new Error("Test error");
return `${i}-${state}`;
}, { maxQueued: 0 });
assertEquals(await throttledFn(0), "0-immediate");
await throttledFn.ready;
await assertRejects(() => throttledFn(1), Error, "Test error");
await throttledFn.ready;
assertEquals(await throttledFn(2), "2-immediate");
await throttledFn.ready;
const promises = Array.from({ length: 4 }).map((_, i) => throttledFn(i));
await throttledFn.ready;
assertEquals(await promises[0], "0-immediate");
assertEquals(await promises[1], "1-discarded");
assertEquals(await promises[2], "2-discarded");
assertEquals(await promises[3], "3-delayed");
assertEquals(await throttledFn(4), "4-immediate");
await throttledFn.ready;
assertEquals(await throttledFn(5), "5-immediate");
await throttledFn.ready;
```