Documentation
    Preparing search index...

    Limits the number of concurrent asynchronous processes that can access a resource.

    A Semaphore is a synchronization object that maintains a count between zero and an optional, specified maximum value. The count is decremented each time a caller enters the Semaphore, and incremented each time a caller releases the Semaphore.

    To enter the Semaphore, call wait or waitAsync. To release the Semaphore, call release. When count reaches zero, subsequent calls to waitAsync will block asynchronously until the Semaphore is released elsewhere. If multiple callers are blocked asynchronously, there is no guaranteed order, such as FIFO or LIFO, that controls who enter the Semaphore.

    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    Properties

    "[toStringTag]": "Semaphore"

    Accessors

    Methods

    • Releases the Semaphore a specified number of times.

      Parameters

      • OptionalreleaseCount: number

        The number of times to exit the Semaphore. undefined will exit the Semaphore once (1).

      Returns number

      Adding the specified count to the Semaphore would cause it to exceed maxCount.

      • A call to this function increments the count by releaseCount. If count is already 0 before this function is called, the function also allows releaseCount Promises blocked by waitAsync to enter the Semaphore.
      • If there are blocked Promises waiting to enter the Semaphore, this function does not guarantee the order in which the blocked Promises enters the Semaphore (e.g. FIFO). That is, the order does not have to be deterministic and is subject to changes in a later version of the implementation.
      • When deciding whether the specified releaseCount will cause Semaphore to exceed maxCount, Promises waiting to enter the Semaphore are not counted in. This behavior is currently just to align with the Win32 ReleaseSemaphore API.
    • Tries to enter the Semaphore immediately.

      Returns boolean

      true if the caller has successfully entered the Semaphore; false otherwise.

      • If the caller has successfully entered the Sempahore, count will be decreased by 1.
      • This method is synchronous and does not block the caller, as JavaScript is single-threaded. Blocking the caller causes deadlocks.
    • Blocks the caller asynchronously until it can enter the Semaphore, reached the specified timeout, or has been aborted.

      Parameters

      • OptionaltimeoutMs: number

        maximum time in milliseconds to wait.

      • Optionalsignal: AbortSignal

        a signal used to cancel the wait.

      Returns Promise<true>

      true if the caller has entered Semaphore successfully; false if the specified timeout has been reched.

      the specified signal has been aborted. If the signal has been aborted with explicit reason, the AbortSignal.reason will be thrown.

      If the caller has successfully entered the Sempahore, count will be decreased by 1.

    • Blocks the caller asynchronously until it can enter the Semaphore or has reached the specified timeout.

      Parameters

      • Optionalsignal: AbortSignal

        a signal used to cancel the wait.

      Returns Promise<boolean>

      always true, as the returned Promise won't fulfill until it enters the Semaphore or has been aborted, which will result in rejection.

      the specified signal has been aborted. If the signal has been aborted with explicit reason, the AbortSignal.reason will be thrown.

      If the caller has successfully entered the Sempahore, count will be decreased by 1.

    • Blocks the caller asynchronously until it can enter the Semaphore.

      Returns Promise<true>

      always true, as the returned Promise won't fulfill until it enters the Semaphore.

      If the caller has successfully entered the Sempahore, count will be decreased by 1.