TS-Scribe - v0.6.2
    Preparing search index...

    Class Semaphore

    A concurrency control mechanism that limits the number of asynchronous tasks accessing a shared resource simultaneously.

    Use Semaphore.acquire to obtain a SemaphoreLock before starting a task, then call lock.release() when the task finishes to free the slot for the next waiting operation.

    The Semaphore.available and Semaphore.waiting getters expose the current state, and Semaphore.size returns the configured concurrency limit.

    Index

    Accessors

    Constructors

    Methods

    Accessors

    • get available(): number

      The number of slots currently available; operations acquired without blocking.

      Returns number

    • get size(): number

      The configured concurrency limit — the maximum number of locks that can be held simultaneously.

      Returns number

    • get waiting(): number

      The number of operations currently waiting for a slot to become available.

      Returns number

    Constructors

    • Creates a new semaphore with the given concurrency limit.

      Parameters

      • size: number

        The maximum number of operations that may run concurrently. Values less than 1 are treated as 1.

      Returns Semaphore

      const semaphore = new Semaphore(3);
      const promises: Promise<void>[] = [];
      for (let i = 0; i < 10; ++i) {
      const lock = await semaphore.acquire();
      const promise = doAsyncTask().finally(lock.release);

      promises.push(promise);
      }

      await Promise.all(promises);

      The loop runs 10 asynchronous tasks, but only 3 will ever run simultaneously.

    Methods

    • Acquires a lock, blocking until one becomes available. The caller must release the returned lock after the operation completes.

      Returns Promise<SemaphoreLock>

      A promise that resolves to a SemaphoreLock once a slot is available.