Mutex, the mutex implementation for async operations

Hierarchy

Constructors - Functional

Methods

Constructors - Functional

  • Mutex, the mutex implementation for async operations

    Parameters

    • defaultAcquireTimeout: number = -1

    Returns Mutex

    Since

    5.15.0

    Example

     const mut = new Mutex()

    async call_api(payload: any) {
    const release = await mut.acquire()
    try {
    // ...
    // this block, will be executed exclusively
    } finally {
    release() // release the mutex
    }
    }

Methods

  • acquire a permit from sem

    Parameters

    • timeout: number = ...

      wait before timeout, if not set, will wait forever

    Returns Promise<ReleaseFunction>

    release function, which used for release a permit to sem

  • run an async function with sem limit

    Type Parameters

    • T

    Parameters

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

      async runner function

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

    • timeout: number = ...

      wait timeout before wait

    Returns Promise<T>