Options
All
  • Public
  • Public/Protected
  • All
Menu

Mutex, the mutex implementation for async operations

Hierarchy

Index

Constructors Functional

Methods

Functional Constructors

  • new Mutex(defaultAcquireTimeout?: number): Mutex
  • Mutex, the mutex implementation for async operations

    since

    5.15.0

    category

    Functional

    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
    }
    }

    Parameters

    • defaultAcquireTimeout: number = -1

    Returns Mutex

Methods

  • acquire(timeout?: number): Promise<ReleaseFunction>
  • 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

  • use<T>(f: () => Promise<T>, timeout?: number): Promise<T>
  • 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>