Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/index.ts>throttle

A JavaScript extension package for building strong and modern applications.
Latest
function throttle
Re-export
import { throttle } from "https://deno.land/x/ayonli_jsext@v0.9.72/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

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

I
Fn extends (this: I, ...args: any[]) => any

Parameters

handler: Fn
duration: number

Examples

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

I
Fn extends (this: I, ...args: any[]) => any

Parameters

handler: Fn
options: ThrottleOptions