<S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<
Success<S[number]>,
Error<S[number]>,
Services<S[number]>
>Runs all streams concurrently until one stream emits its first value, then mirrors that winning stream and interrupts the rest.
Details
Failures or completion from losing streams before a winner is chosen are ignored unless every stream fails or completes before emitting. After a winner is chosen, that stream's later failures are propagated.
Example (Racing multiple streams)
import { Console, Effect, Schedule, Stream } from "effect"
const program = Effect.gen(function*() {
const result = yield* Stream.raceAll(
Stream.fromSchedule(Schedule.spaced("1 second")),
Stream.make(0, 1, 2)
).pipe(Stream.runCollect)
yield* Console.log(result)
})
Effect.runPromise(program)
// Output: [ 0, 1, 2 ]export const const raceAll: <
S extends ReadonlyArray<Stream<any, any, any>>
>(
...streams: S
) => Stream<
Success<S[number]>,
Error<S[number]>,
Services<S[number]>
>
Runs all streams concurrently until one stream emits its first value, then
mirrors that winning stream and interrupts the rest.
Details
Failures or completion from losing streams before a winner is chosen are
ignored unless every stream fails or completes before emitting. After a
winner is chosen, that stream's later failures are propagated.
Example (Racing multiple streams)
import { Console, Effect, Schedule, Stream } from "effect"
const program = Effect.gen(function*() {
const result = yield* Stream.raceAll(
Stream.fromSchedule(Schedule.spaced("1 second")),
Stream.make(0, 1, 2)
).pipe(Stream.runCollect)
yield* Console.log(result)
})
Effect.runPromise(program)
// Output: [ 0, 1, 2 ]
raceAll = <function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S extends interface ReadonlyArray<T>ReadonlyArray<interface Stream<out A, out E = never, out R = never>A Stream<A, E, R> describes a program that can emit many A values, fail
with E, and require R.
Details
Streams are pull-based with backpressure and emit chunks to amortize effect
evaluation. They support monadic composition and error handling similar to
Effect, adapted for multiple values.
Example (Creating and consuming streams)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
yield* Stream.make(1, 2, 3).pipe(
Stream.map((n) => n * 2),
Stream.runForEach((n) => Console.log(n))
)
})
Effect.runPromise(program)
// Output:
// 2
// 4
// 6
Stream<any, any, any>>>(
...streams: S extends ReadonlyArray<Stream<any, any, any>>streams: function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S
): interface Stream<out A, out E = never, out R = never>A Stream<A, E, R> describes a program that can emit many A values, fail
with E, and require R.
Details
Streams are pull-based with backpressure and emit chunks to amortize effect
evaluation. They support monadic composition and error handling similar to
Effect, adapted for multiple values.
Example (Creating and consuming streams)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
yield* Stream.make(1, 2, 3).pipe(
Stream.map((n) => n * 2),
Stream.runForEach((n) => Console.log(n))
)
})
Effect.runPromise(program)
// Output:
// 2
// 4
// 6
Stream<type Success<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _A
: never
Extract the success type from a Stream type.
Example (Extracting the success type from a Stream type)
import type { Stream } from "effect"
type NumberStream = Stream.Stream<number, string, never>
type SuccessType = Stream.Success<NumberStream>
// SuccessType is number
Success<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>, type Error<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _E
: never
Extract the error type from a Stream type.
Example (Extracting the error type from a Stream type)
import type { Stream } from "effect"
type NumberStream = Stream.Stream<number, string, never>
type ErrorType = Stream.Error<NumberStream>
// ErrorType is string
Error<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>, type Services<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _R
: never
Extract the services type from a Stream type.
Example (Extracting the services type from a Stream type)
import type { Stream } from "effect"
interface Database {
query: (sql: string) => unknown
}
type NumberStream = Stream.Stream<number, string, { db: Database }>
type RequiredServices = Stream.Services<NumberStream>
// RequiredServices is { db: Database }
Services<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>> =>
const fromChannel: <
Arr extends Arr.NonEmptyReadonlyArray<any>,
E,
R
>(
channel: Channel.Channel<
Arr,
E,
void,
unknown,
unknown,
unknown,
R
>
) => Stream<
Arr extends Arr.NonEmptyReadonlyArray<infer A>
? A
: never,
E,
R
>
Creates a stream from a array-emitting Channel.
Example (Creating a stream from an array-emitting channel)
import { Channel, Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const channel = Channel.succeed([1, 2, 3] as const)
const stream = Stream.fromChannel(channel)
const result = yield* Stream.runCollect(stream)
yield* Console.log(result)
})
// Output: [ 1, 2, 3 ]
fromChannel(import ChannelChannel.const fromTransform: <
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
EX,
EnvX,
Env
>(
transform: (
upstream: Pull.Pull<InElem, InErr, InDone>,
scope: Scope.Scope
) => Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, EnvX>,
EX,
Env
>
) => Channel<
OutElem,
Pull.ExcludeDone<OutErr> | EX,
OutDone,
InElem,
InErr,
InDone,
Env | EnvX
>
Creates a Channel from a transformation function that operates on upstream pulls.
Example (Creating channels from transforms)
import { Channel, Effect } from "effect"
const channel = Channel.fromTransform((upstream, scope) =>
Effect.succeed(upstream)
)
fromTransform((_: Pull.Pull<
unknown,
unknown,
unknown,
never
>
(parameter) _: {
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;
}
_, scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope) =>
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(() => {
let let winner:
| Pull.Pull<
Arr.NonEmptyReadonlyArray<
Success<S[number]>
>,
Error<S[number]>,
void,
Services<S[number]>
>
| undefined
winner:
| import PullPull.interface Pull<out A, out E = never, out Done = void, out R = never>An effectful pull step that either produces a value, fails with E, or
signals completion with Cause.Done<Done>.
When to use
Use to model one low-level pull step when a consumer repeatedly evaluates an
effect that may emit a value, fail normally, or signal normal completion
through Cause.Done.
Details
Pull represents completion in the error channel so low-level stream
consumers can distinguish ordinary failures from end-of-input and carry a
leftover value when needed.
Pull<import ArrArr.type NonEmptyReadonlyArray<A> = readonly [
A,
...A[]
]
A readonly array guaranteed to have at least one element.
When to use
Use when non-emptiness must be tracked at the type level while preventing mutation.
Many Array module functions accept or return this type.
Example (Typing a non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyReadonlyArray<number> = [1, 2, 3]
const head: number = nonEmpty[0] // guaranteed to exist
NonEmptyReadonlyArray<type Success<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _A
: never
Extract the success type from a Stream type.
Example (Extracting the success type from a Stream type)
import type { Stream } from "effect"
type NumberStream = Stream.Stream<number, string, never>
type SuccessType = Stream.Success<NumberStream>
// SuccessType is number
Success<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>>, type Error<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _E
: never
Extract the error type from a Stream type.
Example (Extracting the error type from a Stream type)
import type { Stream } from "effect"
type NumberStream = Stream.Stream<number, string, never>
type ErrorType = Stream.Error<NumberStream>
// ErrorType is string
Error<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>, void, type Services<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _R
: never
Extract the services type from a Stream type.
Example (Extracting the services type from a Stream type)
import type { Stream } from "effect"
interface Database {
query: (sql: string) => unknown
}
type NumberStream = Stream.Stream<number, string, { db: Database }>
type RequiredServices = Stream.Services<NumberStream>
// RequiredServices is { db: Database }
Services<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>>
| undefined
const const race: Effect.Effect<
readonly [any, ...any[]],
any,
any
>
const race: {
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;
}
race = import EffectEffect.const raceAll: <
Eff extends Effect<any, any, any>
>(
all: Iterable<Eff>,
options?: {
readonly onWinner?: (options: {
readonly fiber: Fiber<any, any>
readonly index: number
readonly parentFiber: Fiber<any, any>
}) => void
}
) => Effect<
Success<Eff>,
Error<Eff>,
Services<Eff>
>
Runs multiple effects concurrently and returns the first successful result.
When to use
Use when early failures should be ignored until a success occurs
or all effects fail.
Details
Early failures do not finish the race; raceAll keeps waiting until one
effect succeeds or every effect has failed. When one effect succeeds, the
remaining effects are interrupted. If every effect fails, the returned effect
fails with a cause containing the collected failure reasons.
Example (Racing many effects)
import { Duration, Effect } from "effect"
// Multiple effects with different delays
const effect1 = Effect.delay(Effect.succeed("Fast"), Duration.millis(100))
const effect2 = Effect.delay(Effect.succeed("Slow"), Duration.millis(500))
const effect3 = Effect.delay(Effect.succeed("Very Slow"), Duration.millis(1000))
// Race all effects - the first to succeed wins
const raced = Effect.raceAll([effect1, effect2, effect3])
// Result: "Fast" (after ~100ms)
raceAll(streams: S extends ReadonlyArray<Stream<any, any, any>>streams.ReadonlyArray<Stream<any, any, any>>.map<Effect.Effect<readonly [any, ...any[]], any, any>>(callbackfn: (value: Stream<any, any, any>, index: number, array: readonly Stream<any, any, any>[]) => Effect.Effect<readonly [any, ...any[]], any, any>, thisArg?: any): Effect.Effect<readonly [any, ...any[]], any, any>[]Calls a defined callback function on each element of an array, and returns an array that contains the results.
map((stream: Stream<any, any, any>(parameter) stream: {
channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>;
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; <…;
}
stream) => {
const const childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope = 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(scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope)
return import ChannelChannel.const toPullScoped: <
OutElem,
OutErr,
OutDone,
Env
>(
self: Channel<
OutElem,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>,
scope: Scope.Scope
) => Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, Env>,
never,
Env
>
Converts a channel to a Pull within an existing scope.
Example (Converting channels to scoped pulls)
import { Channel, Data, Effect, Scope } from "effect"
class ScopedPullError extends Data.TaggedError("ScopedPullError")<{
readonly reason: string
}> {}
// Create a channel
const numbersChannel = Channel.fromIterable([1, 2, 3])
// Convert to Pull with explicit scope
const scopedPullEffect = Effect.gen(function*() {
const scope = yield* Scope.make()
const pull = yield* Channel.toPullScoped(numbersChannel, scope)
return pull
})
toPullScoped(stream: Stream<any, any, any>(parameter) stream: {
channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>;
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; <…;
}
stream.Stream<any, any, any>.channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>(property) Stream<any, any, any>.channel: {
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; <…;
}
channel, const childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope).Pipeable.pipe<Effect.Effect<Pull.Pull<readonly [any, ...any[]], any, void, any>, never, any>, Effect.Effect<[Pull.Pull<readonly [any, ...any[]], any, void, any>, readonly [any, ...any[]]], any, any>, Effect.Effect<[Pull.Pull<readonly [any, ...any[]], any, void, any>, readonly [any, ...any[]]], any, any>, Effect.Effect<readonly [any, ...any[]], any, any>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<Pull.Pull<readonly [any, ...any[]], any, void, any>, never, any>) => Effect.Effect<[Pull.Pull<readonly [any, ...any[]], any, void, any>, readonly [any, ...any[]]], any, any>, bc: (_: Effect.Effect<[Pull.Pull<readonly [any, ...any[]], any, void, any>, readonly [...]], any, any>) => Effect.Effect<...>, cd: (_: Effect.Effect<...>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)pipe(
import EffectEffect.const flatMap: {
<A, B, E1, R1>(
f: (a: A) => Effect<B, E1, R1>
): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E1 | E, R1 | R>
<A, E, R, B, E1, R1>(
self: Effect<A, E, R>,
f: (a: A) => Effect<B, E1, R1>
): Effect<B, E | E1, R | R1>
}
flatMap((pull: Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>
(parameter) pull: {
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;
}
pull) => import EffectEffect.const zip: {
<A2, E2, R2>(
that: Effect<A2, E2, R2>,
options?:
| {
readonly concurrent?:
| boolean
| undefined
}
| undefined
): <A, E, R>(
self: Effect<A, E, R>
) => Effect<[A, A2], E2 | E, R2 | R>
<A, E, R, A2, E2, R2>(
self: Effect<A, E, R>,
that: Effect<A2, E2, R2>,
options?: {
readonly concurrent?: boolean | undefined
}
): Effect<[A, A2], E | E2, R | R2>
}
zip(import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed(pull: Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>
(parameter) pull: {
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;
}
pull), pull: Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>
(parameter) pull: {
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;
}
pull)),
import EffectEffect.const onExit: {
<A, E, XE = never, XR = never>(
f: (
exit: Exit.Exit<A, E>
) => Effect<void, XE, XR>
): <R>(
self: Effect<A, E, R>
) => Effect<A, E | XE, R | XR>
<A, E, R, XE = never, XR = never>(
self: Effect<A, E, R>,
f: (
exit: Exit.Exit<A, E>
) => Effect<void, XE, XR>
): Effect<A, E | XE, R | XR>
}
onExit((exit: Exit.Exit<
[
Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>,
readonly [any, ...any[]]
],
any
>
exit) => {
if (exit: Exit.Exit<
[
Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>,
readonly [any, ...any[]]
],
any
>
exit._tag: "Success" | "Failure"_tag === "Success") {
if (let winner:
| Pull.Pull<
Arr.NonEmptyReadonlyArray<
Success<S[number]>
>,
Error<S[number]>,
void,
Services<S[number]>
>
| undefined
winner) {
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 childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope, exit: Exit.Success<
[
Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>,
readonly [any, ...any[]]
],
any
>
(parameter) exit: {
_tag: "Success";
value: A;
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;
}
exit)
}
let winner:
| Pull.Pull<
Arr.NonEmptyReadonlyArray<
Success<S[number]>
>,
Error<S[number]>,
void,
Services<S[number]>
>
| undefined
winner = exit: Exit.Success<
[
Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>,
readonly [any, ...any[]]
],
any
>
(parameter) exit: {
_tag: "Success";
value: A;
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;
}
exit.Success<[Pull<readonly [any, ...any[]], any, void, any>, readonly [any, ...any[]]], any>.value: [Pull.Pull<readonly [any, ...any[]], any, void, any>, readonly [any, ...any[]]](property) Success<[Pull<readonly [any, ...any[]], any, void, any>, readonly [any, ...any[]]], any>.value: {
0: Pull.Pull<readonly [any, ...any[]], any, void, any>;
1: readonly [any, ...any[]];
length: 2;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
pop: () => readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any> | undefined;
push: (...items: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => number;
concat: { (...items: Array<ConcatArray<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>>): Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>; (...items: Array<readonly [any, ...any…;
join: (separator?: string) => string;
reverse: () => Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>;
shift: () => readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any> | undefined;
slice: (start?: number, end?: number) => Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>;
sort: (compareFn?: ((a: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, b: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>) => number) | undefined) => [Pull.Pull<readonly [any, ...an…;
splice: { (start: number, deleteCount?: number): Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>; (start: number, deleteCount: number, ...items: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any…;
unshift: (...items: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => number;
indexOf: (searchElement: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, fromIndex?: number) => number;
lastIndexOf: (searchElement: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, fromIndex?: number) => number;
every: { (predicate: (value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, index: number, array: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => value is S, thisArg?: any…;
some: (predicate: (value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, index: number, array: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => unknown, thisArg?: any) => …;
forEach: (callbackfn: (value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, index: number, array: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => void, thisArg?: any) => vo…;
map: (callbackfn: (value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, index: number, array: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => U, thisArg?: any) => Array…;
filter: { (predicate: (value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, index: number, array: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => value is S, thisArg?: any…;
reduce: { (callbackfn: (previousValue: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, currentValue: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, currentIndex: number, array: Array…;
reduceRight: { (callbackfn: (previousValue: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, currentValue: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, currentIndex: number, array: Array…;
find: { (predicate: (value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, index: number, obj: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => value is S, thisArg?: any):…;
findIndex: (predicate: (value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, index: number, obj: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => unknown, thisArg?: any) => nu…;
fill: (value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, start?: number, end?: number) => [Pull.Pull<readonly [any, ...any[]], any, void, any>, readonly [any, ...any[]]];
copyWithin: (target: number, start: number, end?: number) => [Pull.Pull<readonly [any, ...any[]], any, void, any>, readonly [any, ...any[]]];
entries: () => ArrayIterator<[number, readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>;
includes: (searchElement: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, index: number, array: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => U | ReadonlyArra…;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any> | undefined;
findLast: { (predicate: (value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, index: number, array: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => value is S, thisArg?: any…;
findLastIndex: (predicate: (value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, index: number, array: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>) => unknown, thisArg?: any) => …;
toReversed: () => Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>;
toSorted: (compareFn?: ((a: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>, b: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>) => number) | undefined) => Array<readonly [any, ...any[]] …;
toSpliced: { (start: number, deleteCount: number, ...items: Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>): Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>; (start: number,…;
with: (index: number, value: readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>) => Array<readonly [any, ...any[]] | Pull.Pull<readonly [any, ...any[]], any, void, any>>;
}
value[0]
return import EffectEffect.const void: Effect.Effect<void, never, never>(alias) const void: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Returns an effect that succeeds with void.
void
}
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 childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope, exit: Exit.Failure<
[
Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>,
readonly [any, ...any[]]
],
any
>
(parameter) exit: {
_tag: "Failure";
cause: Cause.Cause<E>;
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;
}
exit)
}),
import EffectEffect.const map: {
<A, B>(f: (a: A) => B): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E, R>
<A, E, R, B>(
self: Effect<A, E, R>,
f: (a: A) => B
): Effect<B, E, R>
}
map(([, chunk: readonly [any, ...any[]](parameter) chunk: {
0: any;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<any>>): Array<any>; (...items: Array<any>): Array<any> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<any>;
indexOf: (searchElement: any, fromIndex?: number) => number;
lastIndexOf: (searchElement: any, fromIndex?: number) => number;
every: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: any, index: number, array: ReadonlyArray<any>) => void, thisArg?: any) => void;
map: (callbackfn: (value: any, index: number, array: ReadonlyArray<any>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): Array<S>; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): Array<any> };
reduce: { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any, initialValu…;
reduceRight: { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any, initialValu…;
find: { (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => value is S, thisArg?: any): S | undefined; (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => unknown, thisArg?: any): any };
findIndex: (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, any]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<any>;
includes: (searchElement: any, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: any, index: number, array: Array<any>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => any;
findLast: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): S | undefined; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): any };
findLastIndex: (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any) => number;
toReversed: () => Array<any>;
toSorted: (compareFn?: ((a: any, b: any) => number) | undefined) => Array<any>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<any>): Array<any>; (start: number, deleteCount?: number): Array<any> };
with: (index: number, value: any) => Array<any>;
}
chunk]) => chunk: readonly [any, ...any[]](parameter) chunk: {
0: any;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<any>>): Array<any>; (...items: Array<any>): Array<any> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<any>;
indexOf: (searchElement: any, fromIndex?: number) => number;
lastIndexOf: (searchElement: any, fromIndex?: number) => number;
every: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: any, index: number, array: ReadonlyArray<any>) => void, thisArg?: any) => void;
map: (callbackfn: (value: any, index: number, array: ReadonlyArray<any>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): Array<S>; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): Array<any> };
reduce: { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any, initialValu…;
reduceRight: { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any, initialValu…;
find: { (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => value is S, thisArg?: any): S | undefined; (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => unknown, thisArg?: any): any };
findIndex: (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, any]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<any>;
includes: (searchElement: any, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: any, index: number, array: Array<any>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => any;
findLast: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): S | undefined; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): any };
findLastIndex: (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any) => number;
toReversed: () => Array<any>;
toSorted: (compareFn?: ((a: any, b: any) => number) | undefined) => Array<any>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<any>): Array<any>; (start: number, deleteCount?: number): Array<any> };
with: (index: number, value: any) => Array<any>;
}
chunk)
)
}))
return 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(() => let winner:
| Pull.Pull<
Arr.NonEmptyReadonlyArray<
Success<S[number]>
>,
Error<S[number]>,
void,
Services<S[number]>
>
| undefined
winner ?? const race: Effect.Effect<
readonly [any, ...any[]],
any,
any
>
const race: {
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;
}
race)
})
))