<K, A, E>(self: FiberMap<K, A, E>): Effect.Effect<void, E>Waits for the FiberMap to be empty. This will wait for all currently running fibers to complete.
Example (Waiting for an empty map)
import { Effect, FiberMap } from "effect"
const program = Effect.gen(function*() {
const map = yield* FiberMap.make<string>()
// Add some fibers that will complete after a delay
yield* FiberMap.run(map, "task1", Effect.sleep(1000))
yield* FiberMap.run(map, "task2", Effect.sleep(2000))
console.log("Waiting for all fibers to complete...")
// Wait for the map to be empty
yield* FiberMap.awaitEmpty(map)
console.log("All fibers completed!")
console.log(yield* FiberMap.size(map)) // 0
})export const const awaitEmpty: <K, A, E>(
self: FiberMap<K, A, E>
) => Effect.Effect<void, E>
Waits for the FiberMap to be empty.
This will wait for all currently running fibers to complete.
Example (Waiting for an empty map)
import { Effect, FiberMap } from "effect"
const program = Effect.gen(function*() {
const map = yield* FiberMap.make<string>()
// Add some fibers that will complete after a delay
yield* FiberMap.run(map, "task1", Effect.sleep(1000))
yield* FiberMap.run(map, "task2", Effect.sleep(2000))
console.log("Waiting for all fibers to complete...")
// Wait for the map to be empty
yield* FiberMap.awaitEmpty(map)
console.log("All fibers completed!")
console.log(yield* FiberMap.size(map)) // 0
})
awaitEmpty = <function (type parameter) K in <K, A, E>(self: FiberMap<K, A, E>): Effect.Effect<void, E>K, function (type parameter) A in <K, A, E>(self: FiberMap<K, A, E>): Effect.Effect<void, E>A, function (type parameter) E in <K, A, E>(self: FiberMap<K, A, E>): Effect.Effect<void, E>E>(self: FiberMap<K, A, E>(parameter) self: {
deferred: Deferred.Deferred<void, unknown>;
state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap<K, 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 FiberMap<in out K, out A = unknown, out E = unknown>A FiberMap is a collection of fibers, indexed by a key. When the associated
Scope is closed, all fibers in the map will be interrupted. Fibers are
automatically removed from the map when they complete.
Example (Managing fibers in a map)
import { Effect, FiberMap } from "effect"
// Create a FiberMap with string keys
const program = Effect.gen(function*() {
const map = yield* FiberMap.make<string>()
// Add some fibers to the map
yield* FiberMap.run(map, "task1", Effect.never)
yield* FiberMap.run(map, "task2", Effect.never)
// Get the size of the map
const size = yield* FiberMap.size(map)
console.log(size) // 2
})
FiberMap<function (type parameter) K in <K, A, E>(self: FiberMap<K, A, E>): Effect.Effect<void, E>K, function (type parameter) A in <K, A, E>(self: FiberMap<K, A, E>): Effect.Effect<void, E>A, function (type parameter) E in <K, A, E>(self: FiberMap<K, A, E>): Effect.Effect<void, E>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, function (type parameter) E in <K, A, E>(self: FiberMap<K, A, E>): Effect.Effect<void, E>E> =>
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: FiberMap<K, A, E>(parameter) self: {
deferred: Deferred.Deferred<void, unknown>;
state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap<K, 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.FiberMap<K, A, E>.state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap<K, Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" }state._tag: "Open" | "Closed"_tag === "Open" && import MutableHashMapMutableHashMap.const size: <K, V>(
self: MutableHashMap<K, V>
) => number
Returns the number of key-value pairs in the MutableHashMap.
When to use
Use to read how many entries are currently stored in the mutable hash map.
Example (Checking map size)
import { MutableHashMap } from "effect"
const map = MutableHashMap.empty<string, number>()
console.log(MutableHashMap.size(map)) // 0
MutableHashMap.set(map, "key1", 42)
MutableHashMap.set(map, "key2", 100)
console.log(MutableHashMap.size(map)) // 2
MutableHashMap.remove(map, "key1")
console.log(MutableHashMap.size(map)) // 1
MutableHashMap.clear(map)
console.log(MutableHashMap.size(map)) // 0
size(self: FiberMap<K, A, E>(parameter) self: {
deferred: Deferred.Deferred<void, unknown>;
state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap<K, 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.FiberMap<K, A, E>.state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap<K, Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" }state.backing: MutableHashMap.MutableHashMap<
K,
Fiber.Fiber<A, E>
>
(property) backing: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
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;
}
backing) > 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: <[K, Fiber.Fiber<A, E>]>(self: Iterable<[K, Fiber.Fiber<A, E>]>) => [K, 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: FiberMap<K, A, E>(parameter) self: {
deferred: Deferred.Deferred<void, unknown>;
state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap<K, 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)[1]),
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
})