<K, A = unknown, E = unknown>(): Effect.Effect<
FiberMap<K, A, E>,
never,
Scope.Scope
>Creates a scoped FiberMap for storing fibers by key.
Details
When the associated Scope is closed, all fibers in the map will be
interrupted. You can add fibers to the map using FiberMap.set or
FiberMap.run, and the fibers will be automatically removed from the
FiberMap when they complete.
Example (Creating a scoped FiberMap)
import { Effect, FiberMap } from "effect"
Effect.gen(function*() {
const map = yield* FiberMap.make<string>()
// run some effects and add the fibers to the map
yield* FiberMap.run(map, "fiber a", Effect.never)
yield* FiberMap.run(map, "fiber b", Effect.never)
yield* Effect.sleep(1000)
}).pipe(
Effect.scoped // The fibers will be interrupted when the scope is closed
)export const const make: <
K,
A = unknown,
E = unknown
>() => Effect.Effect<
FiberMap<K, A, E>,
never,
Scope.Scope
>
Creates a scoped FiberMap for storing fibers by key.
Details
When the associated Scope is closed, all fibers in the map will be
interrupted. You can add fibers to the map using FiberMap.set or
FiberMap.run, and the fibers will be automatically removed from the
FiberMap when they complete.
Example (Creating a scoped FiberMap)
import { Effect, FiberMap } from "effect"
Effect.gen(function*() {
const map = yield* FiberMap.make<string>()
// run some effects and add the fibers to the map
yield* FiberMap.run(map, "fiber a", Effect.never)
yield* FiberMap.run(map, "fiber b", Effect.never)
yield* Effect.sleep(1000)
}).pipe(
Effect.scoped // The fibers will be interrupted when the scope is closed
)
make = <function (type parameter) K in <K, A = unknown, E = unknown>(): Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>K, function (type parameter) A in <K, A = unknown, E = unknown>(): Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>A = unknown, function (type parameter) E in <K, A = unknown, E = unknown>(): Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>E = unknown>(): 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<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 = unknown, E = unknown>(): Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>K, function (type parameter) A in <K, A = unknown, E = unknown>(): Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>A, function (type parameter) E in <K, A = unknown, E = unknown>(): Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>E>, never, import ScopeScope.Scope> =>
import EffectEffect.const acquireRelease: <A, E, R, R2>(
acquire: Effect<A, E, R>,
release: (
a: A,
exit: Exit.Exit<unknown, unknown>
) => Effect<unknown, never, R2>,
options?: { readonly interruptible?: boolean }
) => Effect<A, E, R | R2 | Scope>
Constructs a scoped resource from an acquisition effect and a release
finalizer.
When to use
Use to acquire a scoped resource with an explicit release finalizer.
Details
If acquisition succeeds, the release finalizer is added to the current scope
and is guaranteed to run when that scope closes. The finalizer receives the
Exit value used to close the scope.
By default, acquisition is protected by an uninterruptible region. Pass
{ interruptible: true } to allow the acquisition effect to be interrupted.
Example (Acquiring and releasing a resource)
import { Console, Effect, Exit } from "effect"
// Simulate a resource that needs cleanup
interface FileHandle {
readonly path: string
readonly content: string
}
// Acquire a file handle
const acquire = Effect.gen(function*() {
yield* Console.log("Opening file")
return { path: "/tmp/file.txt", content: "file content" }
})
// Release the file handle
const release = (handle: FileHandle, exit: Exit.Exit<unknown, unknown>) =>
Console.log(
`Closing file ${handle.path} with exit: ${
Exit.isSuccess(exit) ? "success" : "failure"
}`
)
// Create a scoped resource
const resource = Effect.acquireRelease(acquire, release)
// Use the resource within a scope
const program = Effect.scoped(
Effect.gen(function*() {
const handle = yield* resource
yield* Console.log(`Using file: ${handle.path}`)
return handle.content
})
)
acquireRelease(
import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(() =>
const makeUnsafe: <
K,
A = unknown,
E = unknown
>(
backing: MutableHashMap.MutableHashMap<
K,
Fiber.Fiber<A, E>
>,
deferred: Deferred.Deferred<void, E>
) => FiberMap<K, A, E>
makeUnsafe<function (type parameter) K in <K, A = unknown, E = unknown>(): Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>K, function (type parameter) A in <K, A = unknown, E = unknown>(): Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>A, function (type parameter) E in <K, A = unknown, E = unknown>(): Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>E>(
import MutableHashMapMutableHashMap.const empty: <K, V>() => MutableHashMap<
K,
V
>
Creates an empty MutableHashMap.
When to use
Use to create a fresh mutable map before adding entries over time.
Details
Each call returns a new empty map instance.
Example (Creating an empty map)
import { MutableHashMap } from "effect"
const map = MutableHashMap.empty<string, number>()
// Add some entries
MutableHashMap.set(map, "key1", 42)
MutableHashMap.set(map, "key2", 100)
console.log(MutableHashMap.size(map)) // 2
empty(),
import DeferredDeferred.const makeUnsafe: <
A,
E = never
>() => Deferred<A, E>
Creates an empty Deferred synchronously outside the Effect runtime.
When to use
Use to allocate a Deferred synchronously when direct allocation outside
Effect is required.
Example (Creating a Deferred unsafely)
import { Deferred } from "effect"
const deferred = Deferred.makeUnsafe<number>()
console.log(deferred)
makeUnsafe()
)
),
(map: FiberMap<K, A, E>(parameter) map: {
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;
}
map) =>
import EffectEffect.const suspend: <A, E, R>(
effect: LazyArg<Effect<A, E, R>>
) => Effect<A, E, R>
Creates an Effect lazily, delaying construction until it is needed.
When to use
Use when you need to defer the evaluation of an effect until it is required.
Details
suspend takes a thunk that represents an effect and delays creating it
until the suspended effect is evaluated. This is useful for optimizing
expensive computations, managing circular dependencies such as recursive
functions, and helping TypeScript unify return types when branches construct
different effects. Any side effects or scoped captures inside the thunk are
re-executed on each invocation.
Example (Lazily evaluating side effects)
import { Effect } from "effect"
let i = 0
const bad = Effect.succeed(i++)
const good = Effect.suspend(() => Effect.succeed(i++))
console.log(Effect.runSync(bad)) // Output: 0
console.log(Effect.runSync(bad)) // Output: 0
console.log(Effect.runSync(good)) // Output: 1
console.log(Effect.runSync(good)) // Output: 2
Example (Suspending recursive Fibonacci evaluation)
import { Effect } from "effect"
const blowsUp = (n: number): Effect.Effect<number> =>
n < 2
? Effect.succeed(1)
: Effect.zipWith(blowsUp(n - 1), blowsUp(n - 2), (a, b) => a + b)
// console.log(Effect.runSync(blowsUp(32)))
// crash: JavaScript heap out of memory
const allGood = (n: number): Effect.Effect<number> =>
n < 2
? Effect.succeed(1)
: Effect.zipWith(
Effect.suspend(() => allGood(n - 1)),
Effect.suspend(() => allGood(n - 2)),
(a, b) => a + b
)
console.log(Effect.runSync(allGood(32)))
// Output: 3524578
Example (Helping TypeScript infer recursive effect types)
import { Effect } from "effect"
// Without suspend, TypeScript may struggle with type inference.
// Inferred type:
// (a: number, b: number) =>
// Effect<never, Error, never> | Effect<number, never, never>
const withoutSuspend = (a: number, b: number) =>
b === 0
? Effect.fail(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
// Using suspend to unify return types.
// Inferred type:
// (a: number, b: number) => Effect<number, Error, never>
const withSuspend = (a: number, b: number) =>
Effect.suspend(() =>
b === 0
? Effect.fail(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
)
suspend(() => {
const const state:
| {
readonly _tag: "Closed"
}
| {
readonly _tag: "Open"
readonly backing: MutableHashMap.MutableHashMap<
K,
Fiber.Fiber<A, E>
>
}
state = map: FiberMap<K, A, E>(parameter) map: {
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;
}
map.FiberMap<K, A, E>.state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap<K, Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" }state
if (const state:
| {
readonly _tag: "Closed"
}
| {
readonly _tag: "Open"
readonly backing: MutableHashMap.MutableHashMap<
K,
Fiber.Fiber<A, E>
>
}
state._tag: "Open" | "Closed"_tag === "Closed") return import EffectEffect.const void: Effect.Effect<void, never, never>(alias) const void: {
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;
}
Returns an effect that succeeds with void.
void
map: FiberMap<K, A, E>(parameter) map: {
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;
}
map.FiberMap<K, A, E>.state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap<K, Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" }state = { _tag: "Closed"_tag: "Closed" }
return import FiberFiber.const interruptAll: <
A extends Iterable<Fiber<any, any>>
>(
fibers: A
) => Effect<void>
Interrupts all fibers in the provided iterable, causing them to stop executing
and clean up any acquired resources.
When to use
Use when you need to cancel several forked fibers and wait for their cleanup
to complete.
Details
The current fiber is recorded as the interruptor. The returned Effect
completes only after all interrupted fibers have completed.
Gotchas
Interruption is cooperative for each fiber. The returned Effect can wait for
uninterruptible work and finalizers in any interrupted fiber.
Example (Interrupting multiple fibers)
import { Console, Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
// Create multiple long-running fibers
const fiber1 = yield* Effect.forkChild(
Effect.gen(function*() {
yield* Effect.sleep("5 seconds")
yield* Console.log("Task 1 completed")
return "result1"
})
)
const fiber2 = yield* Effect.forkChild(
Effect.gen(function*() {
yield* Effect.sleep("3 seconds")
yield* Console.log("Task 2 completed")
return "result2"
})
)
const fiber3 = yield* Effect.forkChild(
Effect.gen(function*() {
yield* Effect.sleep("4 seconds")
yield* Console.log("Task 3 completed")
return "result3"
})
)
// Wait a bit, then interrupt all fibers
yield* Effect.sleep("1 second")
yield* Console.log("Interrupting all fibers...")
yield* Fiber.interruptAll([fiber1, fiber2, fiber3])
yield* Console.log("All fibers have been interrupted")
})
interruptAll(import MutableHashMapMutableHashMap.const values: <K, V>(
self: MutableHashMap<K, V>
) => Iterable<V>
Returns an iterable over the values in the MutableHashMap.
When to use
Use to iterate over the values currently stored in a mutable hash map.
Example (Reading values)
import { MutableHashMap } from "effect"
const map = MutableHashMap.make(
["apple", 1],
["banana", 2],
["cherry", 3]
)
const allValues = Array.from(MutableHashMap.values(map))
console.log(allValues) // [1, 2, 3]
// Useful for calculations
const total = allValues.reduce((sum, value) => sum + value, 0)
console.log(total) // 6
// Filter values
const largeValues = allValues.filter((value) => value > 1)
console.log(largeValues) // [2, 3]
values(const state: {
readonly _tag: "Open"
readonly backing: MutableHashMap.MutableHashMap<
K,
Fiber.Fiber<A, E>
>
}
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)).Pipeable.pipe<Effect.Effect<void, never, never>, Effect.Effect<boolean, never, never>>(this: Effect.Effect<void, never, never>, ab: (_: Effect.Effect<void, never, never>) => Effect.Effect<boolean, never, never>): Effect.Effect<boolean, never, never> (+21 overloads)pipe(
import DeferredDeferred.const into: {
<A, E>(deferred: Deferred<A, E>): <R>(
self: Effect<A, E, R>
) => Effect<boolean, never, R>
<A, E, R>(
self: Effect<A, E, R>,
deferred: Deferred<A, E>
): Effect<boolean, never, R>
}
into(map: FiberMap<K, A, E>(parameter) map: {
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;
}
map.FiberMap<in out K, out A = unknown, out E = unknown>.deferred: Deferred.Deferred<void, unknown>(property) FiberMap<in out K, out A = unknown, out E = unknown>.deferred: {
effect: Effect<A, E>;
resumes: Array<(effect: Effect<A, E>) => void> | undefined;
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; <…;
}
deferred)
)
})
)