<R, ER>(
layer: Layer.Layer<R, ER, never>,
options?: { readonly memoMap?: Layer.MemoMap | undefined } | undefined
): ManagedRuntime<R, ER>Creates a ManagedRuntime from a layer.
When to use
Use to create a reusable runtime from a Layer for application entry points
or integration code that runs many effects without rebuilding services.
Details
The layer is built lazily on first use and its context is cached for
subsequent runs. Resources acquired by the layer are owned by the runtime and
are released when dispose or disposeEffect is run. options.memoMap can
be used to share layer memoization with other layer builds.
Gotchas
Dispose the runtime when it is no longer needed. A runtime cannot be reused after disposal.
Example (Creating a managed runtime)
import { Context, Effect, Layer, ManagedRuntime } from "effect"
class Notifications extends Context.Service<Notifications, {
readonly notify: (message: string) => Effect.Effect<void>
}>()("Notifications") {
static readonly layer = Layer.succeed(this)({
notify: Effect.fn("Notifications.notify")((message) =>
Effect.sync(() => console.log(message))
)
})
}
const runtime = ManagedRuntime.make(Notifications.layer)
const program = Effect.flatMap(
Notifications,
(_) => _.notify("Hello, world!")
).pipe(Effect.ensuring(runtime.disposeEffect))
runtime.runPromise(program)
// Hello, world!export const const make: <R, ER>(
layer: Layer.Layer<R, ER, never>,
options?:
| {
readonly memoMap?:
| Layer.MemoMap
| undefined
}
| undefined
) => ManagedRuntime<R, ER>
Creates a ManagedRuntime from a layer.
When to use
Use to create a reusable runtime from a Layer for application entry points
or integration code that runs many effects without rebuilding services.
Details
The layer is built lazily on first use and its context is cached for
subsequent runs. Resources acquired by the layer are owned by the runtime and
are released when dispose or disposeEffect is run. options.memoMap can
be used to share layer memoization with other layer builds.
Gotchas
Dispose the runtime when it is no longer needed. A runtime cannot be reused
after disposal.
Example (Creating a managed runtime)
import { Context, Effect, Layer, ManagedRuntime } from "effect"
class Notifications extends Context.Service<Notifications, {
readonly notify: (message: string) => Effect.Effect<void>
}>()("Notifications") {
static readonly layer = Layer.succeed(this)({
notify: Effect.fn("Notifications.notify")((message) =>
Effect.sync(() => console.log(message))
)
})
}
const runtime = ManagedRuntime.make(Notifications.layer)
const program = Effect.flatMap(
Notifications,
(_) => _.notify("Hello, world!")
).pipe(Effect.ensuring(runtime.disposeEffect))
runtime.runPromise(program)
// Hello, world!
make = <function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R, function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER>(
layer: Layer.Layer<R, ER, never>(parameter) layer: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<R>, ER, never>;
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; <…;
}
layer: import LayerLayer.interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R, function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER, never>,
options: | {
readonly memoMap?: Layer.MemoMap | undefined
}
| undefined
options?: {
readonly memoMap?: Layer.MemoMap | undefinedmemoMap?: import LayerLayer.MemoMap | undefined
} | undefined
): interface ManagedRuntime<in R, out ER>Type helpers associated with ManagedRuntime.
When to use
Use to reference type-level helpers for extracting managed runtime services
and layer errors.
A runtime built from a layer that can execute effects requiring that layer's
services.
When to use
Use as the reusable runtime value returned by make when application entry
points or integration code need to run many effects against the same
layer-built services.
Details
The runtime builds and caches its service context and owns the scope for
resources acquired by the layer.
Gotchas
Dispose the runtime with dispose or disposeEffect when it is no longer
needed.
ManagedRuntime<function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R, function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER> => {
const const memoMap: Layer.MemoMapconst memoMap: {
get: <RIn, E, ROut>(layer: Layer<ROut, E, RIn>, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn> | undefined;
getOrElseMemoize: <RIn, E, ROut>(layer: Layer<ROut, E, RIn>, scope: Scope.Scope, build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>) => Effect<Context.Context<ROut>, E, RIn>;
}
memoMap = options: | {
readonly memoMap?: Layer.MemoMap | undefined
}
| undefined
options?.memoMap?: Layer.MemoMap | undefinedmemoMap ?? import LayerLayer.const makeMemoMapUnsafe: () => MemoMapConstructs a MemoMap synchronously so it can be used to build additional layers.
Example (Creating a memo map unsafely)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
// Create a memo map for manual layer building
const program = Effect.gen(function*() {
const memoMap = Layer.makeMemoMapUnsafe()
const scope = yield* Effect.scope
const dbLayer = Layer.succeed(Database, {
query: Effect.fn("Database.query")((sql: string) => Effect.succeed("result"))
})
const context = yield* Layer.buildWithMemoMap(dbLayer, memoMap, scope)
return Context.get(context, Database)
})
makeMemoMapUnsafe()
const const scope: Scope.Closeableconst scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope = import ScopeScope.const makeUnsafe: (
finalizerStrategy?: "sequential" | "parallel"
) => Closeable
Creates a new Scope synchronously without wrapping it in an Effect.
This is useful when you need a scope immediately but should be used with caution
as it doesn't provide the same safety guarantees as the Effect-wrapped version.
When to use
Use when a scope must be allocated synchronously and the caller will close it
manually.
Example (Creating a scope synchronously)
import { Console, Effect, Exit, Scope } from "effect"
// Create a scope immediately
const scope = Scope.makeUnsafe("sequential")
// Use it in an Effect program
const program = Effect.gen(function*() {
yield* Scope.addFinalizer(scope, Console.log("Cleanup"))
yield* Scope.close(scope, Exit.void)
})
makeUnsafe("parallel")
const const layerScope: Scope.Closeableconst layerScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
layerScope = import ScopeScope.const forkUnsafe: (
scope: Scope,
finalizerStrategy?: "sequential" | "parallel"
) => Closeable
Creates a closeable child scope synchronously and registers it with a parent scope.
When to use
Use when a child scope must be created synchronously and the caller controls
both parent and child scope lifetimes.
Details
Closing the parent closes the child with the same exit value, and closing the
child detaches it from the parent. The optional finalizer strategy configures
the child scope and defaults to "sequential" when omitted.
Example (Creating a child scope synchronously)
import { Console, Effect, Exit, Scope } from "effect"
const program = Effect.gen(function*() {
const parentScope = Scope.makeUnsafe("sequential")
const childScope = Scope.forkUnsafe(parentScope, "parallel")
// Add finalizers to both scopes
yield* Scope.addFinalizer(parentScope, Console.log("Parent cleanup"))
yield* Scope.addFinalizer(childScope, Console.log("Child cleanup"))
// Close child first, then parent
yield* Scope.close(childScope, Exit.void)
yield* Scope.close(parentScope, Exit.void)
})
forkUnsafe(const scope: Scope.Closeableconst scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope, "sequential")
const const defaultRunOptions: Effect.RunOptionsconst defaultRunOptions: {
signal: AbortSignal | undefined;
scheduler: Scheduler | undefined;
uninterruptible: boolean | undefined;
onFiberStart: ((fiber: Fiber<unknown, unknown>) => void) | undefined;
}
defaultRunOptions: import EffectEffect.RunOptions = {
RunOptions.onFiberStart?: <A, E>(self: Fiber<A, E>) => Fiber<A, E>onFiberStart: import FiberFiber.const runIn: {
(scope: Scope): <A, E>(
self: Fiber<A, E>
) => Fiber<A, E>
<A, E>(self: Fiber<A, E>, scope: Scope): Fiber<
A,
E
>
}
runIn(const scope: Scope.Closeableconst scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope)
}
const const mergeRunOptions: <
O extends Effect.RunOptions
>(
options?: O
) => O
mergeRunOptions = <function (type parameter) O in <O extends Effect.RunOptions>(options?: O): OO extends import EffectEffect.RunOptions>(options: O | undefinedoptions?: function (type parameter) O in <O extends Effect.RunOptions>(options?: O): OO): function (type parameter) O in <O extends Effect.RunOptions>(options?: O): OO =>
options: O | undefinedoptions
? {
...options: O extends Effect.RunOptionsoptions,
RunOptions.onFiberStart?: ((fiber: Fiber.Fiber<unknown, unknown>) => void) | undefinedonFiberStart: options: O extends Effect.RunOptionsoptions.RunOptions.onFiberStart?: ((fiber: Fiber<unknown, unknown>) => void) | undefinedonFiberStart ?
(fiber: Fiber.Fiber<unknown, unknown>(parameter) fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | 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; <…;
}
fiber) => {
const defaultRunOptions: Effect.RunOptionsconst defaultRunOptions: {
signal: AbortSignal | undefined;
scheduler: Scheduler | undefined;
uninterruptible: boolean | undefined;
onFiberStart: ((fiber: Fiber<unknown, unknown>) => void) | undefined;
}
defaultRunOptions.RunOptions.onFiberStart?: ((fiber: Fiber<unknown, unknown>) => void) | undefinedonFiberStart!(fiber: Fiber.Fiber<unknown, unknown>(parameter) fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | 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; <…;
}
fiber)
options: O extends Effect.RunOptionsoptions.RunOptions.onFiberStart?: ((fiber: Fiber<unknown, unknown>) => void) | undefinedonFiberStart!(fiber: Fiber.Fiber<unknown, unknown>(parameter) fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | 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; <…;
}
fiber)
} :
const defaultRunOptions: Effect.RunOptionsconst defaultRunOptions: {
signal: AbortSignal | undefined;
scheduler: Scheduler | undefined;
uninterruptible: boolean | undefined;
onFiberStart: ((fiber: Fiber<unknown, unknown>) => void) | undefined;
}
defaultRunOptions.RunOptions.onFiberStart?: ((fiber: Fiber<unknown, unknown>) => void) | undefinedonFiberStart
}
: const defaultRunOptions: Effect.RunOptionsconst defaultRunOptions: {
signal: AbortSignal | undefined;
scheduler: Scheduler | undefined;
uninterruptible: boolean | undefined;
onFiberStart: ((fiber: Fiber<unknown, unknown>) => void) | undefined;
}
defaultRunOptions as function (type parameter) O in <O extends Effect.RunOptions>(options?: O): OO
let let buildFiber:
| Fiber.Fiber<Context.Context<R>, ER>
| undefined
buildFiber: import FiberFiber.interface Fiber<out A, out E = never>A runtime fiber is a lightweight thread that executes Effects. Fibers are
the unit of concurrency in Effect. They provide a way to run multiple
Effects concurrently while maintaining structured concurrency and
cancellation safety.
When to use
Use to observe, join, interrupt, or coordinate work that has already been
forked.
Details
A fiber exposes both safe Effect-based operations, such as
await
,
join
, and
interrupt
, and low-level runtime fields used by
the scheduler and runtime internals.
Gotchas
Prefer the exported functions in this module over calling interruptUnsafe
or pollUnsafe directly. The unsafe methods are immediate runtime hooks and
do not provide the same Effect-based sequencing guarantees.
Example (Awaiting a forked fiber)
import { Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
// Fork an effect to run in a new fiber
const fiber = yield* Effect.forkChild(Effect.succeed(42))
// Wait for the fiber to complete and get its result
const result = yield* Fiber.await(fiber)
console.log(result) // Exit.succeed(42)
return result
})
The Fiber namespace contains utility types and functions for working with fibers.
It provides type-level utilities for fiber operations and variance encoding.
When to use
Use to reference type-level helpers associated with Fiber.
Details
The namespace currently exposes type-level support used by the Fiber
interface. Runtime operations are exported as module-level functions.
Example (Working with fiber types)
import { Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
// Create a fiber
const fiber = yield* Effect.forkChild(Effect.succeed(42))
// Use namespace types for variance
const typedFiber: Fiber.Fiber<number, never> = fiber
// Access fiber properties
console.log(`Fiber ID: ${fiber.id}`)
// Join the fiber
const result = yield* Fiber.join(fiber)
return result // 42
})
Fiber<import ContextContext.interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R>, function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER> | undefined
const const contextEffect: Effect.Effect<
Context.Context<R>,
ER,
never
>
const contextEffect: {
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;
}
contextEffect = import EffectEffect.const withFiber: <
A,
E = never,
R = never
>(
evaluate: (
fiber: Fiber<unknown, unknown>
) => Effect<A, E, R>
) => Effect<A, E, R>
Provides access to the current fiber within an effect computation.
Example (Reading the current fiber)
import { Effect } from "effect"
const program = Effect.withFiber((fiber) =>
Effect.succeed(`Fiber ID: ${fiber.id}`)
)
Effect.runPromise(program).then(console.log)
// Output: Fiber ID: 1
withFiber<import ContextContext.interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R>, function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER>((fiber: Fiber.Fiber<unknown, unknown>(parameter) fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | 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; <…;
}
fiber) => {
if (!let buildFiber:
| Fiber.Fiber<Context.Context<R>, ER>
| undefined
buildFiber) {
let buildFiber:
| Fiber.Fiber<Context.Context<R>, ER>
| undefined
buildFiber = import EffectEffect.const runFork: <A, E>(
effect: Effect<A, E, never>,
options?: RunOptions | undefined
) => Fiber<A, E>
Runs an effect in the background, returning a fiber that can
be observed or interrupted.
When to use
Use when you need to start an effect in the background and receive a fiber.
Example (Running an effect in the background)
import { Console, Effect, Fiber, Schedule } from "effect"
// ┌─── Effect<number, never, never>
// ▼
const program = Effect.repeat(
Console.log("running..."),
Schedule.spaced("200 millis")
)
// ┌─── RuntimeFiber<number, never>
// ▼
const fiber = Effect.runFork(program)
setTimeout(() => {
Effect.runFork(Fiber.interrupt(fiber))
}, 500)
runFork(
import EffectEffect.const tap: {
<A, B, E2, R2>(
f: (a: NoInfer<A>) => Effect<B, E2, R2>
): <E, R>(
self: Effect<A, E, R>
) => Effect<A, E | E2, R | R2>
<B, E2, R2>(f: Effect<B, E2, R2>): <A, E, R>(
self: Effect<A, E, R>
) => Effect<A, E | E2, R | R2>
<A, E, R, B, E2, R2>(
self: Effect<A, E, R>,
f: (a: NoInfer<A>) => Effect<B, E2, R2>
): Effect<A, E | E2, R | R2>
<A, E, R, B, E2, R2>(
self: Effect<A, E, R>,
f: Effect<B, E2, R2>
): Effect<A, E | E2, R | R2>
}
tap(
import LayerLayer.const buildWithMemoMap: {
(memoMap: MemoMap, scope: Scope.Scope): <
RIn,
E,
ROut
>(
self: Layer<ROut, E, RIn>
) => Effect<Context.Context<ROut>, E, RIn>
<RIn, E, ROut>(
self: Layer<ROut, E, RIn>,
memoMap: MemoMap,
scope: Scope.Scope
): Effect<Context.Context<ROut>, E, RIn>
}
buildWithMemoMap(layer: Layer.Layer<R, ER, never>(parameter) layer: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<R>, ER, never>;
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; <…;
}
layer, const memoMap: Layer.MemoMapconst memoMap: {
get: <RIn, E, ROut>(layer: Layer<ROut, E, RIn>, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn> | undefined;
getOrElseMemoize: <RIn, E, ROut>(layer: Layer<ROut, E, RIn>, scope: Scope.Scope, build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>) => Effect<Context.Context<ROut>, E, RIn>;
}
memoMap, const layerScope: Scope.Closeableconst layerScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
layerScope),
(context: Context.Context<R>(parameter) context: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
context) =>
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 self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefinedcachedContext = context: Context.Context<R>(parameter) context: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
context
})
),
{ ...const defaultRunOptions: Effect.RunOptionsconst defaultRunOptions: {
signal: AbortSignal | undefined;
scheduler: Scheduler | undefined;
uninterruptible: boolean | undefined;
onFiberStart: ((fiber: Fiber<unknown, unknown>) => void) | undefined;
}
defaultRunOptions, RunOptions.scheduler?: Scheduler(property) RunOptions.scheduler?: {
executionMode: "sync" | "async";
shouldYield: (fiber: Fiber.Fiber<unknown, unknown>) => boolean;
makeDispatcher: () => SchedulerDispatcher;
}
scheduler: fiber: Fiber.Fiber<unknown, unknown>(parameter) fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | 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; <…;
}
fiber.Fiber<out A, out E = never>.currentScheduler: Scheduler(property) Fiber<out A, out E = never>.currentScheduler: {
executionMode: "sync" | "async";
shouldYield: (fiber: Fiber.Fiber<unknown, unknown>) => boolean;
makeDispatcher: () => SchedulerDispatcher;
}
currentScheduler }
)
}
return import EffectEffect.const flatten: <A, E, R, E2, R2>(
self: Effect<Effect<A, E, R>, E2, R2>
) => Effect<A, E | E2, R | R2>
Flattens an Effect that produces another Effect into a single effect.
Example (Flattening nested effects)
import { Console, Effect } from "effect"
const nested = Effect.succeed(Effect.succeed("hello"))
const program = Effect.gen(function*() {
const value = yield* Effect.flatten(nested)
yield* Console.log(value)
// Output: hello
})
flatten(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(let buildFiber:
| Fiber.Fiber<Context.Context<R>, ER>
| undefined
let buildFiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | 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; <…;
}
buildFiber))
})
const const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self: interface ManagedRuntime<in R, out ER>Type helpers associated with ManagedRuntime.
When to use
Use to reference type-level helpers for extracting managed runtime services
and layer errors.
A runtime built from a layer that can execute effects requiring that layer's
services.
When to use
Use as the reusable runtime value returned by make when application entry
points or integration code need to run many effects against the same
layer-built services.
Details
The runtime builds and caches its service context and owns the scope for
resources acquired by the layer.
Gotchas
Dispose the runtime with dispose or disposeEffect when it is no longer
needed.
ManagedRuntime<function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R, function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER> = {
[const TypeId: "~effect/ManagedRuntime"TypeId]: const TypeId: "~effect/ManagedRuntime"TypeId,
ManagedRuntime<in R, out ER>.memoMap: Layer.MemoMap(property) ManagedRuntime<in R, out ER>.memoMap: {
get: <RIn, E, ROut>(layer: Layer<ROut, E, RIn>, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn> | undefined;
getOrElseMemoize: <RIn, E, ROut>(layer: Layer<ROut, E, RIn>, scope: Scope.Scope, build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>) => Effect<Context.Context<ROut>, E, RIn>;
}
memoMap,
ManagedRuntime<in R, out ER>.scope: Scope.Closeable(property) ManagedRuntime<in R, out ER>.scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope,
ManagedRuntime<R, ER>.contextEffect: Effect.Effect<Context.Context<R>, ER, never>(property) ManagedRuntime<R, ER>.contextEffect: {
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;
}
contextEffect: const contextEffect: Effect.Effect<
Context.Context<R>,
ER,
never
>
const contextEffect: {
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;
}
contextEffect,
ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefinedcachedContext: var undefinedundefined,
ManagedRuntime<R, ER>.context: () => Promise<Context.Context<R>>context() {
return const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefinedcachedContext === var undefinedundefined ?
import EffectEffect.const runPromise: <A, E>(
effect: Effect<A, E>,
options?: RunOptions | undefined
) => Promise<A>
Executes an effect and returns the result as a Promise.
When to use
Use when you need to execute an effect and work with the
result using Promise syntax, typically for compatibility with other
promise-based code.
If the effect succeeds, the promise will resolve with the result. If the
effect fails, the promise will reject with an error.
Example (Running a successful effect as a Promise)
import { Effect } from "effect"
Effect.runPromise(Effect.succeed(1)).then(console.log)
// Output: 1
Example (Running effects as promises)
//Example: Handling a Failing Effect as a Rejected Promise
import { Effect } from "effect"
Effect.runPromise(Effect.fail("my error")).catch(console.error)
// Output:
// (FiberFailure) Error: my error
runPromise(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.contextEffect: Effect.Effect<Context.Context<R>, ER>(property) ManagedRuntime<R, ER>.contextEffect: {
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;
}
contextEffect) :
var Promise: PromiseConstructorRepresents the completion of an asynchronous operation
Promise.PromiseConstructor.resolve<Context.Context<R>>(value: Context.Context<R>): Promise<Context.Context<R>> (+2 overloads)Creates a new resolved promise for the provided value.
resolve(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefined(property) ManagedRuntime<R, ER>.cachedContext: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
cachedContext)
},
ManagedRuntime<R, ER>.dispose: () => Promise<void>Dispose of the resources associated with the runtime.
When to use
Use to release this runtime's layer resources from Promise-based code.
dispose(): interface Promise<T>Represents the completion of an asynchronous operation
Promise<void> {
return import EffectEffect.const runPromise: <A, E>(
effect: Effect<A, E>,
options?: RunOptions | undefined
) => Promise<A>
Executes an effect and returns the result as a Promise.
When to use
Use when you need to execute an effect and work with the
result using Promise syntax, typically for compatibility with other
promise-based code.
If the effect succeeds, the promise will resolve with the result. If the
effect fails, the promise will reject with an error.
Example (Running a successful effect as a Promise)
import { Effect } from "effect"
Effect.runPromise(Effect.succeed(1)).then(console.log)
// Output: 1
Example (Running effects as promises)
//Example: Handling a Failing Effect as a Rejected Promise
import { Effect } from "effect"
Effect.runPromise(Effect.fail("my error")).catch(console.error)
// Output:
// (FiberFailure) Error: my error
runPromise(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<in R, out ER>.disposeEffect: Effect.Effect<void, never, never>(property) ManagedRuntime<in R, out ER>.disposeEffect: {
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;
}
Dispose of the resources associated with the runtime.
When to use
Use to release this runtime's layer resources from an Effect workflow.
disposeEffect)
},
ManagedRuntime<in R, out ER>.disposeEffect: Effect.Effect<void, never, never>(property) ManagedRuntime<in R, out ER>.disposeEffect: {
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;
}
Dispose of the resources associated with the runtime.
When to use
Use to release this runtime's layer resources from an Effect workflow.
disposeEffect: 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 self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self as type Mutable<T> = { -readonly [P in keyof T]: T[P]; }Removes readonly from all properties of T. Supports arrays, tuples,
and records.
When to use
Use when you need a mutable version of a readonly type.
Details
Only affects the top level; nested properties remain readonly.
Example (Converting shallowly to mutable types)
import type { Types } from "effect"
type Obj = Types.Mutable<{
readonly a: string
readonly b: ReadonlyArray<number>
}>
// { a: string; b: ReadonlyArray<number> }
// ^ mutable ^ still readonly inside
type Arr = Types.Mutable<ReadonlyArray<string>>
// string[]
type Tup = Types.Mutable<readonly [string, number]>
// [string, number]
Mutable<interface ManagedRuntime<in R, out ER>Type helpers associated with ManagedRuntime.
When to use
Use to reference type-level helpers for extracting managed runtime services
and layer errors.
A runtime built from a layer that can execute effects requiring that layer's
services.
When to use
Use as the reusable runtime value returned by make when application entry
points or integration code need to run many effects against the same
layer-built services.
Details
The runtime builds and caches its service context and owns the scope for
resources acquired by the layer.
Gotchas
Dispose the runtime with dispose or disposeEffect when it is no longer
needed.
ManagedRuntime<function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R, function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER>>).contextEffect: Effect.Effect<
Context.Context<R>,
ER,
never
>
(property) contextEffect: {
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;
}
contextEffect = import EffectEffect.const die: (
defect: unknown
) => Effect<never>
Creates an effect that terminates a fiber with a specified error.
When to use
Use when you need an Effect to report an unrecoverable defect instead of a
typed error.
Details
The die function is used to signal a defect, which represents a critical
and unexpected error in the code. When invoked, it produces an effect that
does not handle the error and instead terminates the fiber.
The error channel of the resulting effect is of type never, indicating that
it cannot recover from this failure.
Example (Failing on division by zero)
import { Effect } from "effect"
const divide = (a: number, b: number) =>
b === 0
? Effect.die(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
// ┌─── Effect<number, never, never>
// ▼
const program = divide(1, 0)
Effect.runPromise(program).catch(console.error)
// Output:
// (FiberFailure) Error: Cannot divide by zero
// ...stack trace...
die("ManagedRuntime disposed")
const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefinedcachedContext = var undefinedundefined
return import ScopeScope.const close: <A, E>(
self: Scope,
exit: Exit<A, E>
) => Effect<void>
Closes a scope and runs its registered finalizers.
When to use
Use to close a scope manually with a specific exit value.
Details
Finalizers run in the scope's configured order and receive the supplied
Exit.
Example (Running scope finalizers)
import { Console, Effect, Exit, Scope } from "effect"
const resourceManagement = Effect.gen(function*() {
const scope = yield* Scope.make("sequential")
// Add multiple finalizers
yield* Scope.addFinalizer(scope, Console.log("Close database connection"))
yield* Scope.addFinalizer(scope, Console.log("Close file handle"))
yield* Scope.addFinalizer(scope, Console.log("Release memory"))
// Do some work...
yield* Console.log("Performing operations...")
// Close scope - finalizers run in reverse order of registration
yield* Scope.close(scope, Exit.succeed("Success!"))
// Output: "Release memory", "Close file handle", "Close database connection"
})
close(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<in R, out ER>.scope: Scope.Closeable(property) ManagedRuntime<in R, out ER>.scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope, import ExitExit.const void: Exit.Exit<void, never>Provides a pre-allocated successful Exit with a void value.
When to use
Use when you need a shared successful Exit with no meaningful value.
Details
Equivalent to Exit.succeed(undefined) but shared as a single instance,
avoiding allocation for a common case.
Example (Referencing the void Exit)
import { Exit } from "effect"
const exit = Exit.void
console.log(Exit.isSuccess(exit)) // true
void)
}),
ManagedRuntime<R, ER>.runFork: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>Executes the effect using the provided Scheduler or using the global
Scheduler if not provided
When to use
Use to fork an effect against this runtime's services and get the running
fiber.
runFork<function (type parameter) A in runFork<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Fiber.Fiber<A, E | ER>A, function (type parameter) E in runFork<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Fiber.Fiber<A, E | ER>E>(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect: 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<function (type parameter) A in runFork<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Fiber.Fiber<A, E | ER>A, function (type parameter) E in runFork<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Fiber.Fiber<A, E | ER>E, function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R>, options: Effect.RunOptionsoptions?: import EffectEffect.RunOptions): import FiberFiber.interface Fiber<out A, out E = never>A runtime fiber is a lightweight thread that executes Effects. Fibers are
the unit of concurrency in Effect. They provide a way to run multiple
Effects concurrently while maintaining structured concurrency and
cancellation safety.
When to use
Use to observe, join, interrupt, or coordinate work that has already been
forked.
Details
A fiber exposes both safe Effect-based operations, such as
await
,
join
, and
interrupt
, and low-level runtime fields used by
the scheduler and runtime internals.
Gotchas
Prefer the exported functions in this module over calling interruptUnsafe
or pollUnsafe directly. The unsafe methods are immediate runtime hooks and
do not provide the same Effect-based sequencing guarantees.
Example (Awaiting a forked fiber)
import { Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
// Fork an effect to run in a new fiber
const fiber = yield* Effect.forkChild(Effect.succeed(42))
// Wait for the fiber to complete and get its result
const result = yield* Fiber.await(fiber)
console.log(result) // Exit.succeed(42)
return result
})
The Fiber namespace contains utility types and functions for working with fibers.
It provides type-level utilities for fiber operations and variance encoding.
When to use
Use to reference type-level helpers associated with Fiber.
Details
The namespace currently exposes type-level support used by the Fiber
interface. Runtime operations are exported as module-level functions.
Example (Working with fiber types)
import { Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
// Create a fiber
const fiber = yield* Effect.forkChild(Effect.succeed(42))
// Use namespace types for variance
const typedFiber: Fiber.Fiber<number, never> = fiber
// Access fiber properties
console.log(`Fiber ID: ${fiber.id}`)
// Join the fiber
const result = yield* Fiber.join(fiber)
return result // 42
})
Fiber<function (type parameter) A in runFork<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Fiber.Fiber<A, E | ER>A, function (type parameter) E in runFork<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Fiber.Fiber<A, E | ER>E | function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER> {
return const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefinedcachedContext === var undefinedundefined ?
import EffectEffect.const runFork: <A, E>(
effect: Effect<A, E, never>,
options?: RunOptions | undefined
) => Fiber<A, E>
Runs an effect in the background, returning a fiber that can
be observed or interrupted.
When to use
Use when you need to start an effect in the background and receive a fiber.
Example (Running an effect in the background)
import { Console, Effect, Fiber, Schedule } from "effect"
// ┌─── Effect<number, never, never>
// ▼
const program = Effect.repeat(
Console.log("running..."),
Schedule.spaced("200 millis")
)
// ┌─── RuntimeFiber<number, never>
// ▼
const fiber = Effect.runFork(program)
setTimeout(() => {
Effect.runFork(Fiber.interrupt(fiber))
}, 500)
runFork(function provide<R, ER, A, E>(
managed: ManagedRuntime<R, ER>,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E | ER>
provide(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self, effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect), const mergeRunOptions: <
O extends Effect.RunOptions
>(
options?: O
) => O
mergeRunOptions(options: Effect.RunOptionsoptions)) :
import EffectEffect.const runForkWith: <R>(
context: Context.Context<R>
) => <A, E>(
effect: Effect<A, E, R>,
options?: RunOptions | undefined
) => Fiber<A, E>
Runs an effect in the background with the provided services.
When to use
Use when an effect still requires services, you already have a Context, and
you want a background fiber.
Example (Running with services in the background)
import { Context, Effect } from "effect"
interface Logger {
log: (message: string) => void
}
const Logger = Context.Service<Logger>("Logger")
const services = Context.make(Logger, {
log: (message) => console.log(message)
})
const program = Effect.gen(function*() {
const logger = yield* Logger
logger.log("Hello from service!")
return "done"
})
const fiber = Effect.runForkWith(services)(program)
runForkWith(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefined(property) ManagedRuntime<R, ER>.cachedContext: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
cachedContext)(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect, const mergeRunOptions: <
O extends Effect.RunOptions
>(
options?: O
) => O
mergeRunOptions(options: Effect.RunOptionsoptions))
},
ManagedRuntime<R, ER>.runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void }) => (interruptor?: number | undefined) => voidExecutes the effect asynchronously, eventually passing the exit value to
the specified callback.
When to use
Use when invoking this effectful method at the edges of your
program.
runCallback<function (type parameter) A in runCallback<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E | ER>) => void;
}): (interruptor?: number | undefined) => void
A, function (type parameter) E in runCallback<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E | ER>) => void;
}): (interruptor?: number | undefined) => void
E>(
effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect: 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<function (type parameter) A in runCallback<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E | ER>) => void;
}): (interruptor?: number | undefined) => void
A, function (type parameter) E in runCallback<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E | ER>) => void;
}): (interruptor?: number | undefined) => void
E, function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R>,
options: Effect.RunOptions & {
readonly onExit: (
exit: Exit.Exit<A, E | ER>
) => void
}
options?: import EffectEffect.RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E | ER>) => voidonExit: (exit: Exit.Exit<A, E | ER>exit: import ExitExit.type Exit<A, E = never> = Exit.Success<A, E> | Exit.Failure<A, E>Represents the result of an Effect computation.
When to use
Use when you need to synchronously inspect whether an Effect computation
succeeded or failed.
Details
An Exit<A, E> is either Success<A, E> containing a value of type A, or
Failure<A, E> containing a Cause<E> describing why the computation
failed.
Since Exit is also an Effect, you can yield it inside Effect.gen.
Example (Pattern matching on an Exit)
import { Exit } from "effect"
const success: Exit.Exit<number> = Exit.succeed(42)
const failure: Exit.Exit<number, string> = Exit.fail("error")
const result = Exit.match(success, {
onSuccess: (value) => `Got value: ${value}`,
onFailure: (cause) => `Got error: ${cause}`
})
Namespace containing helper types shared by Exit values.
When to use
Use to reference helper types that describe the shared structure of Exit
values.
Exit<function (type parameter) A in runCallback<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E | ER>) => void;
}): (interruptor?: number | undefined) => void
A, function (type parameter) E in runCallback<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E | ER>) => void;
}): (interruptor?: number | undefined) => void
E | function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER>) => void
}
): (interruptor: number | undefinedinterruptor?: number | undefined) => void {
return const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefinedcachedContext === var undefinedundefined ?
import EffectEffect.const runCallback: <A, E>(
effect: Effect<A, E, never>,
options?:
| (RunOptions & {
readonly onExit: (
exit: Exit.Exit<A, E>
) => void
})
| undefined
) => (interruptor?: number | undefined) => void
Runs an effect asynchronously, registering onExit as a fiber observer and
returning an interruptor.
Details
The interruptor calls fiber.interruptUnsafe with the optional interruptor
id.
Example (Running with a callback)
import { Console, Effect, Exit } from "effect"
const program = Effect.gen(function*() {
yield* Console.log("working")
return "done"
})
const interrupt = Effect.runCallback(program, {
onExit: (exit) => {
Effect.runSync(
Exit.match(exit, {
onFailure: () => Console.log("failed"),
onSuccess: (value) => Console.log(`success: ${value}`)
})
)
}
})
// Output:
// working
// success: done
// interrupt() to cancel the fiber if needed
runCallback(function provide<R, ER, A, E>(
managed: ManagedRuntime<R, ER>,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E | ER>
provide(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self, effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect), const mergeRunOptions: <
O extends Effect.RunOptions
>(
options?: O
) => O
mergeRunOptions(options: Effect.RunOptions & {
readonly onExit: (
exit: Exit.Exit<A, E | ER>
) => void
}
options)) :
import EffectEffect.const runCallbackWith: <R>(
context: Context.Context<R>
) => <A, E>(
effect: Effect<A, E, R>,
options?:
| (RunOptions & {
readonly onExit: (
exit: Exit.Exit<A, E>
) => void
})
| undefined
) => (interruptor?: number | undefined) => void
Forks an effect with the provided services, registers onExit as a fiber observer, and returns an interruptor.
When to use
Use when embedding an effect into callback-style code with explicit services
and a synchronous interruptor.
Details
The returned interruptor calls fiber.interruptUnsafe, optionally with an interruptor id.
Example (Running with services and a callback)
import { Console, Context, Effect, Exit } from "effect"
interface Logger {
log: (message: string) => Effect.Effect<void>
}
const Logger = Context.Service<Logger>("Logger")
const services = Context.make(Logger, {
log: (message) => Console.log(message)
})
const program = Effect.gen(function*() {
const logger = yield* Logger
yield* logger.log("Started")
return "done"
})
const interrupt = Effect.runCallbackWith(services)(program, {
onExit: (exit) => {
if (Exit.isFailure(exit)) {
// handle failure or interruption
}
}
})
// Use the interruptor if you need to cancel the fiber later.
interrupt()
runCallbackWith(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefined(property) ManagedRuntime<R, ER>.cachedContext: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
cachedContext)(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect, const mergeRunOptions: <
O extends Effect.RunOptions
>(
options?: O
) => O
mergeRunOptions(options: Effect.RunOptions & {
readonly onExit: (
exit: Exit.Exit<A, E | ER>
) => void
}
options))
},
ManagedRuntime<R, ER>.runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, E | ER>Executes the effect synchronously returning the exit.
When to use
Use when invoking this effectful method at the edges of your
program.
runSyncExit<function (type parameter) A in runSyncExit<A, E>(effect: Effect.Effect<A, E, R>): Exit.Exit<A, E | ER>A, function (type parameter) E in runSyncExit<A, E>(effect: Effect.Effect<A, E, R>): Exit.Exit<A, E | ER>E>(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect: 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<function (type parameter) A in runSyncExit<A, E>(effect: Effect.Effect<A, E, R>): Exit.Exit<A, E | ER>A, function (type parameter) E in runSyncExit<A, E>(effect: Effect.Effect<A, E, R>): Exit.Exit<A, E | ER>E, function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R>): import ExitExit.type Exit<A, E = never> = Exit.Success<A, E> | Exit.Failure<A, E>Represents the result of an Effect computation.
When to use
Use when you need to synchronously inspect whether an Effect computation
succeeded or failed.
Details
An Exit<A, E> is either Success<A, E> containing a value of type A, or
Failure<A, E> containing a Cause<E> describing why the computation
failed.
Since Exit is also an Effect, you can yield it inside Effect.gen.
Example (Pattern matching on an Exit)
import { Exit } from "effect"
const success: Exit.Exit<number> = Exit.succeed(42)
const failure: Exit.Exit<number, string> = Exit.fail("error")
const result = Exit.match(success, {
onSuccess: (value) => `Got value: ${value}`,
onFailure: (cause) => `Got error: ${cause}`
})
Namespace containing helper types shared by Exit values.
When to use
Use to reference helper types that describe the shared structure of Exit
values.
Exit<function (type parameter) A in runSyncExit<A, E>(effect: Effect.Effect<A, E, R>): Exit.Exit<A, E | ER>A, function (type parameter) E in runSyncExit<A, E>(effect: Effect.Effect<A, E, R>): Exit.Exit<A, E | ER>E | function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER> {
return const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefinedcachedContext === var undefinedundefined ?
import EffectEffect.const runSyncExit: <A, E>(
effect: Effect<A, E>
) => Exit.Exit<A, E>
Runs an effect synchronously and captures the outcome safely as an Exit type, which
represents the outcome (success or failure) of the effect.
When to use
Use to find out whether an effect succeeded or failed,
including any defects, without dealing with asynchronous operations.
Details
The Exit type represents the result of the effect. Successful effects are
wrapped in Success, and failed effects are wrapped in Failure with a
Cause.
If the effect contains asynchronous operations, runSyncExit will
return an Failure with a Die cause, indicating that the effect cannot be
resolved synchronously.
Example (Observing synchronous results as Exit)
import { Effect } from "effect"
console.log(Effect.runSyncExit(Effect.succeed(1)))
// Output:
// {
// _id: "Exit",
// _tag: "Success",
// value: 1
// }
console.log(Effect.runSyncExit(Effect.fail("my error")))
// Output:
// {
// _id: "Exit",
// _tag: "Failure",
// cause: {
// _id: "Cause",
// _tag: "Fail",
// failure: "my error"
// }
// }
Example (Capturing async work as a Die cause)
import { Effect } from "effect"
console.log(Effect.runSyncExit(Effect.promise(() => Promise.resolve(1))))
// Output:
// {
// _id: 'Exit',
// _tag: 'Failure',
// cause: {
// _id: 'Cause',
// _tag: 'Die',
// defect: [Fiber #0 cannot be resolved synchronously. This is caused by using runSync on an effect that performs async work] {
// fiber: [FiberRuntime],
// _tag: 'AsyncFiberException',
// name: 'AsyncFiberException'
// }
// }
// }
runSyncExit(function provide<R, ER, A, E>(
managed: ManagedRuntime<R, ER>,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E | ER>
provide(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self, effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect)) :
import EffectEffect.const runSyncExitWith: <R>(
context: Context.Context<R>
) => <A, E>(
effect: Effect<A, E, R>
) => Exit.Exit<A, E>
Runs an effect synchronously with provided services, returning an Exit result safely.
When to use
Use when you already have a Context and need a synchronous Exit instead of
throwing on failure.
Example (Running synchronously with services as Exit)
import { Context, Effect, Exit } from "effect"
// Define a logger service
const Logger = Context.Service<{
log: (msg: string) => void
}>("Logger")
const program = Effect.gen(function*() {
const logger = yield* Effect.service(Logger)
logger.log("Computing result...")
return 42
})
// Prepare context
const context = Context.make(Logger, {
log: (msg) => console.log(`[LOG] ${msg}`)
})
const exit = Effect.runSyncExitWith(context)(program)
if (Exit.isSuccess(exit)) {
console.log(`Success: ${exit.value}`)
} else {
console.log(`Failure: ${exit.cause}`)
}
// Output:
// [LOG] Computing result...
// Success: 42
runSyncExitWith(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefined(property) ManagedRuntime<R, ER>.cachedContext: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
cachedContext)(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect)
},
ManagedRuntime<R, ER>.runSync: <A, E>(effect: Effect.Effect<A, E, R>) => AExecutes the effect synchronously throwing in case of errors or async boundaries.
When to use
Use when invoking this effectful method at the edges of your
program.
runSync<function (type parameter) A in runSync<A, E>(effect: Effect.Effect<A, E, R>): AA, function (type parameter) E in runSync<A, E>(effect: Effect.Effect<A, E, R>): AE>(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect: 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<function (type parameter) A in runSync<A, E>(effect: Effect.Effect<A, E, R>): AA, function (type parameter) E in runSync<A, E>(effect: Effect.Effect<A, E, R>): AE, function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R>): function (type parameter) A in runSync<A, E>(effect: Effect.Effect<A, E, R>): AA {
return const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefinedcachedContext === var undefinedundefined ?
import EffectEffect.const runSync: <A, E>(
effect: Effect<A, E>
) => A
Executes an effect synchronously and returns its success value.
When to use
Use when you need to execute an effect that is guaranteed to complete
synchronously.
Details
If the effect fails, dies, is interrupted, or performs asynchronous work,
runSync throws a FiberFailure instead of returning a value. Use
runSyncExit when you want the failure captured as an Exit.
Example (Running a synchronous effect)
import { Effect } from "effect"
const program = Effect.sync(() => {
console.log("Hello, World!")
return 1
})
const result = Effect.runSync(program)
// Output: Hello, World!
console.log(result)
// Output: 1
Example (Throwing for failed or async effects)
import { Effect } from "effect"
try {
// Attempt to run an effect that fails
Effect.runSync(Effect.fail("my error"))
} catch (e) {
console.error(e)
}
// Output:
// (FiberFailure) Error: my error
try {
// Attempt to run an effect that involves async work
Effect.runSync(Effect.promise(() => Promise.resolve(1)))
} catch (e) {
console.error(e)
}
// Output:
// (FiberFailure) AsyncFiberException: Fiber #0 cannot be resolved synchronously. This is caused by using runSync on an effect that performs async work
runSync(function provide<R, ER, A, E>(
managed: ManagedRuntime<R, ER>,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E | ER>
provide(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self, effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect)) :
import EffectEffect.const runSyncWith: <R>(
context: Context.Context<R>
) => <A, E>(effect: Effect<A, E, R>) => A
Executes an effect synchronously with provided services.
When to use
Use when you already have a Context, the effect is known to complete
synchronously, and failures should throw.
Example (Running synchronously with services)
import { Context, Effect } from "effect"
interface MathService {
add: (a: number, b: number) => number
}
const MathService = Context.Service<MathService>("MathService")
const context = Context.make(MathService, {
add: (a, b) => a + b
})
const program = Effect.gen(function*() {
const math = yield* MathService
return math.add(2, 3)
})
const result = Effect.runSyncWith(context)(program)
console.log(result) // 5
runSyncWith(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefined(property) ManagedRuntime<R, ER>.cachedContext: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
cachedContext)(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect)
},
ManagedRuntime<R, ER>.runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, E | ER>>Runs the Effect, returning a JavaScript Promise that will be resolved
with the Exit state of the effect once the effect has been executed.
When to use
Use when invoking this effectful method at the edges of your
program.
runPromiseExit<function (type parameter) A in runPromiseExit<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Promise<Exit.Exit<A, E | ER>>A, function (type parameter) E in runPromiseExit<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Promise<Exit.Exit<A, E | ER>>E>(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect: 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<function (type parameter) A in runPromiseExit<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Promise<Exit.Exit<A, E | ER>>A, function (type parameter) E in runPromiseExit<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Promise<Exit.Exit<A, E | ER>>E, function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R>, options: Effect.RunOptionsoptions?: import EffectEffect.RunOptions): interface Promise<T>Represents the completion of an asynchronous operation
Promise<import ExitExit.type Exit<A, E = never> = Exit.Success<A, E> | Exit.Failure<A, E>Represents the result of an Effect computation.
When to use
Use when you need to synchronously inspect whether an Effect computation
succeeded or failed.
Details
An Exit<A, E> is either Success<A, E> containing a value of type A, or
Failure<A, E> containing a Cause<E> describing why the computation
failed.
Since Exit is also an Effect, you can yield it inside Effect.gen.
Example (Pattern matching on an Exit)
import { Exit } from "effect"
const success: Exit.Exit<number> = Exit.succeed(42)
const failure: Exit.Exit<number, string> = Exit.fail("error")
const result = Exit.match(success, {
onSuccess: (value) => `Got value: ${value}`,
onFailure: (cause) => `Got error: ${cause}`
})
Namespace containing helper types shared by Exit values.
When to use
Use to reference helper types that describe the shared structure of Exit
values.
Exit<function (type parameter) A in runPromiseExit<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Promise<Exit.Exit<A, E | ER>>A, function (type parameter) E in runPromiseExit<A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions): Promise<Exit.Exit<A, E | ER>>E | function (type parameter) ER in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
ER>> {
return const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefinedcachedContext === var undefinedundefined ?
import EffectEffect.const runPromiseExit: <A, E>(
effect: Effect<A, E>,
options?: RunOptions | undefined
) => Promise<Exit.Exit<A, E>>
Runs an effect and returns a Promise that resolves to an Exit, which
represents the outcome (success or failure) of the effect.
When to use
Use when you need to determine if an effect succeeded
or failed, including any defects, and you want to work with a Promise.
Details
The Exit type represents the result of the effect. Successful effects are
wrapped in Success, and failed effects are wrapped in Failure with a
Cause.
Example (Observing promise results as Exit)
import { Effect } from "effect"
// Execute a successful effect and get the Exit result as a Promise
Effect.runPromiseExit(Effect.succeed(1)).then(console.log)
// Output:
// {
// _id: "Exit",
// _tag: "Success",
// value: 1
// }
// Execute a failing effect and get the Exit result as a Promise
Effect.runPromiseExit(Effect.fail("my error")).then(console.log)
// Output:
// {
// _id: "Exit",
// _tag: "Failure",
// cause: {
// _id: "Cause",
// _tag: "Fail",
// failure: "my error"
// }
// }
runPromiseExit(function provide<R, ER, A, E>(
managed: ManagedRuntime<R, ER>,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E | ER>
provide(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self, effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect), const mergeRunOptions: <
O extends Effect.RunOptions
>(
options?: O
) => O
mergeRunOptions(options: Effect.RunOptionsoptions)) :
import EffectEffect.const runPromiseExitWith: <R>(
context: Context.Context<R>
) => <A, E>(
effect: Effect<A, E, R>,
options?: RunOptions | undefined
) => Promise<Exit.Exit<A, E>>
Runs an effect and returns a Promise of Exit with provided services.
When to use
Use when you already have a Context and need Promise interop that preserves
success and failure as an Exit.
Example (Running with services as an Exit promise)
import { Context, Effect, Exit } from "effect"
interface Database {
query: (sql: string) => string
}
const Database = Context.Service<Database>("Database")
const services = Context.make(Database, {
query: (sql) => `Result for: ${sql}`
})
const program = Effect.gen(function*() {
const db = yield* Database
return db.query("SELECT * FROM users")
})
Effect.runPromiseExitWith(services)(program).then((exit) => {
if (Exit.isSuccess(exit)) {
console.log("Success:", exit.value)
}
})
runPromiseExitWith(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefined(property) ManagedRuntime<R, ER>.cachedContext: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
cachedContext)(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect, const mergeRunOptions: <
O extends Effect.RunOptions
>(
options?: O
) => O
mergeRunOptions(options: Effect.RunOptionsoptions))
},
ManagedRuntime<R, ER>.runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: { readonly signal?: AbortSignal | undefined }) => Promise<A>Runs the Effect, returning a JavaScript Promise that will be resolved
with the value of the effect once the effect has been executed, or will be
rejected with the first error or exception throw by the effect.
When to use
Use when invoking this effectful method at the edges of your
program.
runPromise<function (type parameter) A in runPromise<A, E>(effect: Effect.Effect<A, E, R>, options?: {
readonly signal?: AbortSignal | undefined;
}): Promise<A>
A, function (type parameter) E in runPromise<A, E>(effect: Effect.Effect<A, E, R>, options?: {
readonly signal?: AbortSignal | undefined;
}): Promise<A>
E>(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect: 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<function (type parameter) A in runPromise<A, E>(effect: Effect.Effect<A, E, R>, options?: {
readonly signal?: AbortSignal | undefined;
}): Promise<A>
A, function (type parameter) E in runPromise<A, E>(effect: Effect.Effect<A, E, R>, options?: {
readonly signal?: AbortSignal | undefined;
}): Promise<A>
E, function (type parameter) R in <R, ER>(layer: Layer.Layer<R, ER, never>, options?: {
readonly memoMap?: Layer.MemoMap | undefined;
} | undefined): ManagedRuntime<R, ER>
R>, options: | {
readonly signal?: AbortSignal | undefined
}
| undefined
options?: {
readonly signal?: AbortSignal | undefinedsignal?: AbortSignal | undefined
}): interface Promise<T>Represents the completion of an asynchronous operation
Promise<function (type parameter) A in runPromise<A, E>(effect: Effect.Effect<A, E, R>, options?: {
readonly signal?: AbortSignal | undefined;
}): Promise<A>
A> {
return const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefinedcachedContext === var undefinedundefined ?
import EffectEffect.const runPromise: <A, E>(
effect: Effect<A, E>,
options?: RunOptions | undefined
) => Promise<A>
Executes an effect and returns the result as a Promise.
When to use
Use when you need to execute an effect and work with the
result using Promise syntax, typically for compatibility with other
promise-based code.
If the effect succeeds, the promise will resolve with the result. If the
effect fails, the promise will reject with an error.
Example (Running a successful effect as a Promise)
import { Effect } from "effect"
Effect.runPromise(Effect.succeed(1)).then(console.log)
// Output: 1
Example (Running effects as promises)
//Example: Handling a Failing Effect as a Rejected Promise
import { Effect } from "effect"
Effect.runPromise(Effect.fail("my error")).catch(console.error)
// Output:
// (FiberFailure) Error: my error
runPromise(function provide<R, ER, A, E>(
managed: ManagedRuntime<R, ER>,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E | ER>
provide(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self, effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect), const mergeRunOptions: <
O extends Effect.RunOptions
>(
options?: O
) => O
mergeRunOptions(options: | {
readonly signal?: AbortSignal | undefined
}
| undefined
options)) :
import EffectEffect.const runPromiseWith: <R>(
context: Context.Context<R>
) => <A, E>(
effect: Effect<A, E, R>,
options?: RunOptions | undefined
) => Promise<A>
Executes an effect as a Promise with the provided services.
When to use
Use when you already have a Context and need Promise interop that rejects on
effect failure.
Example (Running with services as a promise)
import { Context, Effect } from "effect"
interface Config {
apiUrl: string
}
const Config = Context.Service<Config>("Config")
const context = Context.make(Config, {
apiUrl: "https://api.example.com"
})
const program = Effect.gen(function*() {
const config = yield* Config
return `Connecting to ${config.apiUrl}`
})
Effect.runPromiseWith(context)(program).then(console.log)
runPromiseWith(const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self.ManagedRuntime<R, ER>.cachedContext: Context.Context<R> | undefined(property) ManagedRuntime<R, ER>.cachedContext: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
cachedContext)(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect, const mergeRunOptions: <
O extends Effect.RunOptions
>(
options?: O
) => O
mergeRunOptions(options: | {
readonly signal?: AbortSignal | undefined
}
| undefined
options))
}
}
return const self: ManagedRuntime<R, ER>const self: {
memoMap: Layer.MemoMap;
contextEffect: Effect.Effect<Context.Context<R>, ER>;
context: () => Promise<Context.Context<R>>;
scope: Scope.Closeable;
cachedContext: Context.Context<R> | undefined;
runFork: <A, E>(self: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Fiber.Fiber<A, E | ER>;
runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>;
runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A;
runCallback: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions & { readonly onExit: (exit: Exit.Exit<A, E | ER>) => void } | undefined) => (interruptor?: number | undefined) => void;
runPromise: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<A>;
runPromiseExit: <A, E>(effect: Effect.Effect<A, E, R>, options?: Effect.RunOptions) => Promise<Exit.Exit<A, ER | E>>;
dispose: () => Promise<void>;
disposeEffect: Effect.Effect<void, never, never>;
}
self
}