<A, E>(self: FiberSet<A, E>): Effect.Effect<void>Waits until the fiber set is empty.
Example (Waiting for an empty set)
import { Effect, FiberSet } from "effect"
const program = Effect.gen(function*() {
const set = yield* FiberSet.make()
// Add some fibers that will complete
yield* FiberSet.run(set, Effect.sleep(100))
yield* FiberSet.run(set, Effect.sleep(200))
// Wait for all fibers to complete
yield* FiberSet.awaitEmpty(set)
console.log(yield* FiberSet.size(set)) // 0
})export const const awaitEmpty: <A, E>(
self: FiberSet<A, E>
) => Effect.Effect<void>
Waits until the fiber set is empty.
Example (Waiting for an empty set)
import { Effect, FiberSet } from "effect"
const program = Effect.gen(function*() {
const set = yield* FiberSet.make()
// Add some fibers that will complete
yield* FiberSet.run(set, Effect.sleep(100))
yield* FiberSet.run(set, Effect.sleep(200))
// Wait for all fibers to complete
yield* FiberSet.awaitEmpty(set)
console.log(yield* FiberSet.size(set)) // 0
})
awaitEmpty = <function (type parameter) A in <A, E>(self: FiberSet<A, E>): Effect.Effect<void>A, function (type parameter) E in <A, E>(self: FiberSet<A, E>): Effect.Effect<void>E>(self: FiberSet<A, E>(parameter) self: {
deferred: Deferred.Deferred<void, unknown>;
state: { readonly _tag: "Open"; readonly backing: Set<Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" };
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: interface FiberSet<out A = unknown, out E = unknown>A FiberSet is a collection of fibers that can be managed together.
When the associated Scope is closed, all fibers in the set will be interrupted.
Example (Managing fibers in a set)
import { Effect, FiberSet } from "effect"
const program = Effect.gen(function*() {
const set = yield* FiberSet.make<string, string>()
// Add fibers to the set
yield* FiberSet.run(set, Effect.succeed("hello"))
yield* FiberSet.run(set, Effect.succeed("world"))
// Wait for all fibers to complete
yield* FiberSet.awaitEmpty(set)
})
FiberSet<function (type parameter) A in <A, E>(self: FiberSet<A, E>): Effect.Effect<void>A, function (type parameter) E in <A, E>(self: FiberSet<A, E>): Effect.Effect<void>E>): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<void> =>
import EffectEffect.const whileLoop: <A, E, R>(options: {
readonly while: LazyArg<boolean>
readonly body: LazyArg<Effect<A, E, R>>
readonly step: (a: A) => void
}) => Effect<void, E, R>
Executes a body effect repeatedly while a condition holds true.
Example (Repeating an effectful loop)
import { Effect } from "effect"
let counter = 0
const program = Effect.whileLoop({
while: () => counter < 5,
body: () => Effect.sync(() => ++counter),
step: (n) => console.log(`Current count: ${n}`)
})
Effect.runPromise(program)
// Output:
// Current count: 1
// Current count: 2
// Current count: 3
// Current count: 4
// Current count: 5
whileLoop({
while: LazyArg<boolean>while: () => self: FiberSet<A, E>(parameter) self: {
deferred: Deferred.Deferred<void, unknown>;
state: { readonly _tag: "Open"; readonly backing: Set<Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" };
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self.FiberSet<A, E>.state: { readonly _tag: "Open"; readonly backing: Set<Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" }state._tag: "Open" | "Closed"_tag === "Open" && self: FiberSet<A, E>(parameter) self: {
deferred: Deferred.Deferred<void, unknown>;
state: { readonly _tag: "Open"; readonly backing: Set<Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" };
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self.FiberSet<A, E>.state: { readonly _tag: "Open"; readonly backing: Set<Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" }state.backing: Set<Fiber.Fiber<A, E>>backing.Set<T>.size: numbersize > 0,
body: LazyArg<
Effect.Effect<Exit.Exit<A, E>, never, never>
>
body: () => import FiberFiber.await<A, E>(self: Fiber<A, E>): Effect<Exit<A, E>>Waits for a fiber to complete and returns its exit value.
When to use
Use when you need to inspect whether the fiber succeeded,
failed, died, or was interrupted without propagating the failure.
Details
The returned Effect always succeeds with an Exit describing the fiber's
outcome.
Gotchas
This does not flatten the fiber result into the current Effect. Use
join
when you want fiber failures to fail the current Effect.
Example (Awaiting a fiber exit)
import { Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
const fiber = yield* Effect.forkChild(Effect.succeed(42))
const exit = yield* Fiber.await(fiber)
console.log(exit) // Exit.succeed(42)
})
await(import IterableIterable.const headUnsafe: <Fiber.Fiber<A, E>>(self: Iterable<Fiber.Fiber<A, E>>) => Fiber.Fiber<A, E>Gets the first element of an Iterable without returning an Option.
When to use
Use when the Iterable is known to be non-empty and direct access to the
first element is preferred over handling Option.none.
Gotchas
Throws if the Iterable is empty.
Example (Getting the first element unsafely)
import { Iterable } from "effect"
const numbers = [1, 2, 3]
console.log(Iterable.headUnsafe(numbers)) // 1
const letters = "hello"
console.log(Iterable.headUnsafe(letters)) // "h"
// Iterable.headUnsafe(Iterable.empty<number>())
// throws Error: "headUnsafe: empty iterable"
// Use only when you're certain the iterable is non-empty
const nonEmpty = Iterable.range(1, 10)
console.log(Iterable.headUnsafe(nonEmpty)) // 1
headUnsafe(self: FiberSet<A, E>(parameter) self: {
deferred: Deferred.Deferred<void, unknown>;
state: { readonly _tag: "Open"; readonly backing: Set<Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" };
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self)),
step: LazyArg<void>step: const constVoid: LazyArg<void>Returns no meaningful value when called.
When to use
Use when you need a thunk that is called only for its effect and has no
meaningful return value.
Example (Returning void from a thunk)
import { Function } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Function.constVoid(), undefined)
constVoid
})