import { throttle } from "https://deno.land/x/ayonli_jsext@v0.9.72/workerd/index.ts";
Creates a throttled function that will only be run once in a certain amount of time.
If a subsequent call happens within the duration
(in milliseconds), the
previous result will be returned and the handler
function will not be
invoked.
Examples
Example 1
Example 1
import throttle from "@ayonli/jsext/throttle";
import { sleep } from "@ayonli/jsext/async";
const fn = throttle((input: string) => input, 1_000);
console.log(fn("foo")); // foo
console.log(fn("bar")); // foo
await sleep(1_000);
console.log(fn("bar")); // bar
Type Parameters
Fn extends (this: I, ...args: any[]) => any
Parameters
handler: Fn
Examples
Example 1
Example 1
import throttle from "@ayonli/jsext/throttle";
import { sleep } from "@ayonli/jsext/async";
const out1 = await throttle(() => Promise.resolve("foo"), {
duration: 1_000,
for: "example",
})();
console.log(out1); // foo
const out2 = await throttle(() => Promise.resolve("bar"), {
duration: 1_000,
for: "example",
})();
console.log(out2); // foo
await sleep(1_000);
const out3 = await throttle(() => Promise.resolve("bar"), {
duration: 1_000,
for: "example",
})();
console.log(out3); // bar
Type Parameters
Fn extends (this: I, ...args: any[]) => any
Parameters
handler: Fn