crnt - v0.0.31
    Preparing search index...

    Function newSemaphore

    • Creates a new Semaphore with the given number of permits.

      Parameters

      • permits: number

        The number of permits the semaphore has.

      Returns Semaphore

      A new Semaphore instance.

      const semaphore = newSemaphore(2);

      // Acquire a permit manually
      const permit = await semaphore.acquire();
      try {
      // Critical section - permit is automatically managed
      await fetch('/api/data');
      } finally {
      permit.release();
      }

      // Or use with automatic resource management
      await using permit = await semaphore.acquire();
      // Critical section - permit automatically released at end of scope
      await fetch('/api/data');

      // Or use the run helper
      await semaphore.run(async () => {
      // Critical section - permit automatically managed
      return await fetch('/api/data');
      });