Hyperlinkv0.8.0-beta.28

Effect

Effect.withConcurrencyconsteffect/Effect.ts:6345
(concurrency: number | "unbounded"): <A, E, R>(
  self: Effect<A, E, R>
) => Effect<A, E, R>
<A, E, R>(
  self: Effect<A, E, R>,
  concurrency: number | "unbounded"
): Effect<A, E, R>

Sets the concurrency level for parallel operations within an effect.

Example (Setting local concurrency)

import { Console, Effect } from "effect"

const task = (id: number) =>
  Effect.gen(function*() {
    yield* Console.log(`Task ${id} starting`)
    yield* Effect.sleep("100 millis")
    yield* Console.log(`Task ${id} completed`)
    return id
  })

// Run tasks with limited concurrency (max 2 at a time)
const program = Effect.gen(function*() {
  const tasks = [1, 2, 3, 4, 5].map(task)
  return yield* Effect.all(tasks, { concurrency: 2 })
}).pipe(
  Effect.withConcurrency(2)
)

Effect.runPromise(program).then(console.log)
// Tasks will run with max 2 concurrent operations
// [1, 2, 3, 4, 5]
references
Source effect/Effect.ts:63459 lines
export const withConcurrency: {
  (
    concurrency: number | "unbounded"
  ): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>
  <A, E, R>(
    self: Effect<A, E, R>,
    concurrency: number | "unbounded"
  ): Effect<A, E, R>
} = internal.withConcurrency