Options
All
  • Public
  • Public/Protected
  • All
Menu

Class LiveLimit

The limiter.

You likely want to create your limiter as a global to ensure that all operations will share the same limiter and will respect the limit.

Limitations: You can only queue up to 2^54 - 1 many calls at a time (over 18 quadrillion). Additional calls may cause some queued calls to be dropped. Although nodejs will probably break before you get to that number.

Hierarchy

  • LiveLimit

Index

Constructors

Methods

Constructors

Methods

  • limit<T>(fn: (() => Promise<T>)): Promise<null | T>
  • Calls the function when a spot is open based on the limiter rules.

    The returned promise is resolved:

    • When the function is executed and the promise returned by the function resolves
    • If maxReached is Drop, when the function call is dropped.

    Accepts no arguments, you should use closures to pass parameters to the function.

    example
    // Setup
    const LIMITER = new LiveLimit({ maxLive: 3 });
    async function myFunc(n: number) {
    // ... use n here ...
    }

    // Usage
    const param = 1;
    LIMITER.limit(async () => {
    await myFunc(param);
    });
    // Usage when you have many calls to make at once
    const params = [1, 2, 3, 4, 5, 6];
    params.forEach((param) => {
    LIMITER.limit(async () => {
    myFunc(param);
    });
    });

    Type Parameters

    • T

    Parameters

    • fn: (() => Promise<T>)

      The function to execute.

        • (): Promise<T>
        • Returns Promise<T>

    Returns Promise<null | T>

    The resolved promise returned by the function, or null if the call was dropped.

Generated using TypeDoc