<S, A, E, R>(
s: S,
f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>
): Stream<A, E, R>Creates a stream by repeatedly applying an effectful step function to a state.
Details
Each readonly [value, nextState] result emits value and continues with
nextState; returning undefined ends the stream.
Example (Unfolding stream state)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const stream = Stream.unfold(1, (n) => Effect.succeed([n, n + 1] as const))
const values = yield* Stream.runCollect(stream.pipe(Stream.take(5)))
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 1, 2, 3, 4, 5 ]export const const unfold: <S, A, E, R>(
s: S,
f: (
s: S
) => Effect.Effect<
readonly [A, S] | undefined,
E,
R
>
) => Stream<A, E, R>
Creates a stream by repeatedly applying an effectful step function to a
state.
Details
Each readonly [value, nextState] result emits value and continues with
nextState; returning undefined ends the stream.
Example (Unfolding stream state)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const stream = Stream.unfold(1, (n) => Effect.succeed([n, n + 1] as const))
const values = yield* Stream.runCollect(stream.pipe(Stream.take(5)))
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 1, 2, 3, 4, 5 ]
unfold = <function (type parameter) S in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>S, function (type parameter) A in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>A, function (type parameter) E in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>E, function (type parameter) R in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>R>(
s: Ss: function (type parameter) S in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>S,
f: (
s: S
) => Effect.Effect<
readonly [A, S] | undefined,
E,
R
>
f: (s: Ss: function (type parameter) S in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>S) => 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<readonly [function (type parameter) A in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>A, function (type parameter) S in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>S] | undefined, function (type parameter) E in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>E, function (type parameter) R in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>R>
): 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<function (type parameter) A in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>A, function (type parameter) E in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>E, function (type parameter) R in <S, A, E, R>(s: S, f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>): Stream<A, E, R>R> =>
const fromPull: <A, E, R, EX, RX>(
pull: Effect.Effect<
Pull.Pull<
Arr.NonEmptyReadonlyArray<A>,
E,
void,
R
>,
EX,
RX
>
) => Stream<A, Pull.ExcludeDone<E> | EX, R | RX>
Creates a stream from a pull effect, such as one produced by Stream.toPull.
Details
A pull effect yields chunks on demand and completes when the upstream stream ends.
See Stream.toPull for a matching producer.
Example (Creating a stream from a pull effect)
import { Console, Effect, Stream } from "effect"
const program = Effect.scoped(
Effect.gen(function*() {
const source = Stream.make(1, 2, 3)
const pull = yield* Stream.toPull(source)
const stream = Stream.fromPull(Effect.succeed(pull))
const values = yield* Stream.runCollect(stream)
yield* Console.log(values)
})
)
Effect.runPromise(program)
// Output: [1, 2, 3]
fromPull(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 state: Sstate = s: Ss
return 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(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(() => f: (
s: S
) => Effect.Effect<
readonly [A, S] | undefined,
E,
R
>
f(let state: Sstate)), (next: readonly [A, S] | undefinednext) => {
if (next: readonly [A, S] | undefinednext === var undefinedundefined) return import CauseCause.const done: <A = void>(
value?: A
) => Effect.Effect<never, Done<A>>
Creates an Effect that fails with a Done error. Shorthand for
Effect.fail(Cause.Done(value)).
When to use
Use when you model stream or queue completion through the error channel.
Example (Failing with Done)
import { Cause, Effect } from "effect"
const program = Cause.done("finished")
Effect.runPromiseExit(program).then((exit) => {
console.log(exit._tag) // "Failure"
})
done()
let state: Sstate = next: readonly [A, S](parameter) next: {
0: A;
1: S;
length: 2;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<S | A>>): Array<S | A>; (...items: Array<S | A | ConcatArray<S | A>>): Array<S | A> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<S | A>;
indexOf: (searchElement: S | A, fromIndex?: number) => number;
lastIndexOf: (searchElement: S | A, fromIndex?: number) => number;
every: { (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: S | A, index: number, array: ReadonlyArray<S | A>) => void, thisArg?: any) => void;
map: (callbackfn: (value: S | A, index: number, array: ReadonlyArray<S | A>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => value is S, thisArg?: any): Array<S>; (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => unknown, thisArg?: any): Array<S | A> };
reduce: { (callbackfn: (previousValue: S | A, currentValue: S | A, currentIndex: number, array: ReadonlyArray<S | A>) => S | A): S | A; (callbackfn: (previousValue: S | A, currentValue: S | A, currentIndex: number, array: ReadonlyArray<S | A>) => …;
reduceRight: { (callbackfn: (previousValue: S | A, currentValue: S | A, currentIndex: number, array: ReadonlyArray<S | A>) => S | A): S | A; (callbackfn: (previousValue: S | A, currentValue: S | A, currentIndex: number, array: ReadonlyArray<S | A>) => …;
find: { (predicate: (value: S | A, index: number, obj: ReadonlyArray<S | A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: S | A, index: number, obj: ReadonlyArray<S | A>) => unknown, thisArg?: any): S | A | undefined };
findIndex: (predicate: (value: S | A, index: number, obj: ReadonlyArray<S | A>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, S | A]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<S | A>;
includes: (searchElement: S | A, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: S | A, index: number, array: Array<S | A>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => S | A | undefined;
findLast: { (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => unknown, thisArg?: any): S | A | undefined };
findLastIndex: (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => unknown, thisArg?: any) => number;
toReversed: () => Array<S | A>;
toSorted: (compareFn?: ((a: S | A, b: S | A) => number) | undefined) => Array<S | A>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<S | A>): Array<S | A>; (start: number, deleteCount?: number): Array<S | A> };
with: (index: number, value: S | A) => Array<S | A>;
}
next[1]
return 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(import ArrArr.const of: <A>(a: A) => NonEmptyArray<A>Wraps a single value in a NonEmptyArray.
Example (Creating a single-element array)
import { Array } from "effect"
console.log(Array.of(1)) // [1]
of(next: readonly [A, S](parameter) next: {
0: A;
1: S;
length: 2;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<S | A>>): Array<S | A>; (...items: Array<S | A | ConcatArray<S | A>>): Array<S | A> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<S | A>;
indexOf: (searchElement: S | A, fromIndex?: number) => number;
lastIndexOf: (searchElement: S | A, fromIndex?: number) => number;
every: { (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: S | A, index: number, array: ReadonlyArray<S | A>) => void, thisArg?: any) => void;
map: (callbackfn: (value: S | A, index: number, array: ReadonlyArray<S | A>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => value is S, thisArg?: any): Array<S>; (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => unknown, thisArg?: any): Array<S | A> };
reduce: { (callbackfn: (previousValue: S | A, currentValue: S | A, currentIndex: number, array: ReadonlyArray<S | A>) => S | A): S | A; (callbackfn: (previousValue: S | A, currentValue: S | A, currentIndex: number, array: ReadonlyArray<S | A>) => …;
reduceRight: { (callbackfn: (previousValue: S | A, currentValue: S | A, currentIndex: number, array: ReadonlyArray<S | A>) => S | A): S | A; (callbackfn: (previousValue: S | A, currentValue: S | A, currentIndex: number, array: ReadonlyArray<S | A>) => …;
find: { (predicate: (value: S | A, index: number, obj: ReadonlyArray<S | A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: S | A, index: number, obj: ReadonlyArray<S | A>) => unknown, thisArg?: any): S | A | undefined };
findIndex: (predicate: (value: S | A, index: number, obj: ReadonlyArray<S | A>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, S | A]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<S | A>;
includes: (searchElement: S | A, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: S | A, index: number, array: Array<S | A>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => S | A | undefined;
findLast: { (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => unknown, thisArg?: any): S | A | undefined };
findLastIndex: (predicate: (value: S | A, index: number, array: ReadonlyArray<S | A>) => unknown, thisArg?: any) => number;
toReversed: () => Array<S | A>;
toSorted: (compareFn?: ((a: S | A, b: S | A) => number) | undefined) => Array<S | A>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<S | A>): Array<S | A>; (start: number, deleteCount?: number): Array<S | A> };
with: (index: number, value: S | A) => Array<S | A>;
}
next[0]))
})
}))