happy-rusty
    Preparing search index...

    Function Semaphore

    • Creates a new Semaphore with the given capacity.

      Parameters

      • permits: number

        The maximum number of concurrent operations allowed. Must be a non-negative integer. Use 0 to disallow any concurrent acquire (acquire will wait forever).

      Returns Semaphore

      A new Semaphore instance.

      If permits is negative or not an integer.

      // Limit to 5 concurrent operations
      const sem = Semaphore(5);

      // Binary semaphore (equivalent to a value-less Mutex)
      const binary = Semaphore(1);
      // Task queue: process 2 jobs at a time
      const sem = Semaphore(2);

      async function processJob(job: Job) {
      return sem.withPermit(async () => {
      return await runJob(job);
      });
      }

      await Promise.all(jobs.map(processJob));