Hyperlinkv0.8.0-beta.28

Semaphore

Semaphore.makeUnsafeconsteffect/Semaphore.ts:190
(permits: number): Semaphore

Creates a Semaphore synchronously with the specified total number of permits.

When to use

Use to construct a semaphore synchronously when an immediate value is required outside an Effect workflow.

Example (Creating an unsafe semaphore)

import { Effect, Semaphore } from "effect"

const semaphore = Semaphore.makeUnsafe(3)

const task = (id: number) =>
  semaphore.withPermits(1)(
    Effect.gen(function*() {
      yield* Effect.log(`Task ${id} started`)
      yield* Effect.sleep("1 second")
      yield* Effect.log(`Task ${id} completed`)
    })
  )

// Only 3 tasks can run concurrently
const program = Effect.all([
  task(1),
  task(2),
  task(3),
  task(4),
  task(5)
], { concurrency: "unbounded" })
constructors
export const makeUnsafe = (permits: number): Semaphore => new SemaphoreImpl(permits)
Referenced by 5 symbols