Constructors

  • Parameters

    • permits: number

      The number of initial permits.

    Returns Semaphore

    Throws

    If the given number of permits is negative.

Methods

  • Acquires a number of permits, waiting until enough permits are available.

    Parameters

    • permits: number = 1

      The number of permits to acquire.

    Returns Promise<void>

    A promise that isn't resolved until the requested number of permits is available.

    The promise will never be rejected.

    Throws

    If the given number of permits is negative.

  • Returns number

    The available number of permits; never negative.

  • Drains the number of permits. Afterwards, availablePermits() will return 0.

    Returns number

    The number of available permits before this method was called.

  • Returns boolean

    true if at least one promise returned by one of the acquire or tryAcquire methods has not yet been resolved, or false otherwise.

  • Releases a number of permits.

    Parameters

    • permits: number = 1

      The number of permits to release.

    Returns void

    Throws

    If the given number of permits is negative.

  • Returns string

  • Acquires a single permit if one is available.

    Returns boolean

    true if at least one permit is available, or false otherwise.

  • Acquires a number permit if enough are available.

    Parameters

    • permits: number

      The number of permits to acquire.

    Returns boolean

    true if enough permits are available, or false otherwise.

    Throws

    If the given number of permits is negative.

  • Tries to acquire a number of permits, waiting until enough are available.

    Parameters

    Returns Promise<boolean>

    A promise that isn't resolved until enough permits are available or the given timeout expires, whichever occurs first. If enough permits become available before the timeout expires, the promise will be resolved with true. If the timeout expires or is not positive, the promise will be resolved with false. If the timeout is not positive, the promise will be resolved immediately.

    The promise will never be rejected.

    Throws

    If the given options specifies a negative number of permits.

  • Returns number

    The number of promises returned by one of the acquire or tryAcquire methods that have not yet been resolved.