<B, E2, R2>(options: {
readonly duration: Duration.Input
readonly orElse: () => Stream<B, E2, R2>
}): <A, E, R>(self: Stream<A, E, R>) => Stream<A | B, E | E2, R | R2>
<A, E, R, B, E2, R2>(
self: Stream<A, E, R>,
options: {
readonly duration: Duration.Input
readonly orElse: () => Stream<B, E2, R2>
}
): Stream<A | B, E | E2, R | R2>Switches to a fallback stream if this stream does not emit a value within the specified duration.
When to use
Use when a stream should continue with another stream if an upstream pull waits longer than the allowed duration.
Details
The timeout is checked for each pull. A zero duration uses orElse
immediately, while an infinite duration leaves the original stream
unchanged.
Gotchas
The fallback stream is not timed after the switch.
export const const timeoutOrElse: {
<B, E2, R2>(options: {
readonly duration: Duration.Input
readonly orElse: () => Stream<B, E2, R2>
}): <A, E, R>(
self: Stream<A, E, R>
) => Stream<A | B, E | E2, R | R2>
<A, E, R, B, E2, R2>(
self: Stream<A, E, R>,
options: {
readonly duration: Duration.Input
readonly orElse: () => Stream<B, E2, R2>
}
): Stream<A | B, E | E2, R | R2>
}
Switches to a fallback stream if this stream does not emit a value within
the specified duration.
When to use
Use when a stream should continue with another stream if an upstream pull
waits longer than the allowed duration.
Details
The timeout is checked for each pull. A zero duration uses orElse
immediately, while an infinite duration leaves the original stream
unchanged.
Gotchas
The fallback stream is not timed after the switch.
timeoutOrElse: {
<function (type parameter) B in <B, E2, R2>(options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): <A, E, R>(self: Stream<A, E, R>) => Stream<A | B, E | E2, R | R2>
B, function (type parameter) E2 in <B, E2, R2>(options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): <A, E, R>(self: Stream<A, E, R>) => Stream<A | B, E | E2, R | R2>
E2, function (type parameter) R2 in <B, E2, R2>(options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): <A, E, R>(self: Stream<A, E, R>) => Stream<A | B, E | E2, R | R2>
R2>(options: {
readonly duration: Duration.Input
readonly orElse: () => Stream<B, E2, R2>
}
options: {
readonly duration: Duration.Inputduration: import DurationDuration.type Input =
| number
| bigint
| Duration.Duration
| readonly [seconds: number, nanos: number]
| `${number} nano`
| `${number} nanos`
| `${number} micro`
| `${number} micros`
| `${number} milli`
| `${number} millis`
| `${number} second`
| `${number} seconds`
| `${number} minute`
| `${number} minutes`
| `${number} hour`
| `${number} hours`
| `${number} day`
| `${number} days`
| `${number} week`
| `${number} weeks`
| "Infinity"
| "-Infinity"
| Duration.DurationObject
Valid input types that can be converted to a Duration.
When to use
Use when an API should accept any value that Effect can convert into a
Duration, including existing durations, millisecond numbers, nanosecond
bigints, high-resolution tuples, duration strings, infinity strings, or
duration objects.
Details
String inputs accept values like "10 seconds", "500 millis",
"Infinity", and "-Infinity". Finite fractional values that are
normalized to nanoseconds are rounded to the nearest nanosecond, with ties
away from zero.
Input
readonly orElse: () => Stream<B, E2, R2>orElse: () => 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) B in <B, E2, R2>(options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): <A, E, R>(self: Stream<A, E, R>) => Stream<A | B, E | E2, R | R2>
B, function (type parameter) E2 in <B, E2, R2>(options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): <A, E, R>(self: Stream<A, E, R>) => Stream<A | B, E | E2, R | R2>
E2, function (type parameter) R2 in <B, E2, R2>(options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): <A, E, R>(self: Stream<A, E, R>) => Stream<A | B, E | E2, R | R2>
R2>
}): <function (type parameter) A in <A, E, R>(self: Stream<A, E, R>): Stream<A | B, E | E2, R | R2>A, function (type parameter) E in <A, E, R>(self: Stream<A, E, R>): Stream<A | B, E | E2, R | R2>E, function (type parameter) R in <A, E, R>(self: Stream<A, E, R>): Stream<A | B, E | E2, R | R2>R>(self: Stream<A, E, R>(parameter) self: {
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; <…;
}
self: 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 <A, E, R>(self: Stream<A, E, R>): Stream<A | B, E | E2, R | R2>A, function (type parameter) E in <A, E, R>(self: Stream<A, E, R>): Stream<A | B, E | E2, R | R2>E, function (type parameter) R in <A, E, R>(self: Stream<A, E, R>): Stream<A | B, E | E2, R | R2>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 <A, E, R>(self: Stream<A, E, R>): Stream<A | B, E | E2, R | R2>A | function (type parameter) B in <B, E2, R2>(options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): <A, E, R>(self: Stream<A, E, R>) => Stream<A | B, E | E2, R | R2>
B, function (type parameter) E in <A, E, R>(self: Stream<A, E, R>): Stream<A | B, E | E2, R | R2>E | function (type parameter) E2 in <B, E2, R2>(options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): <A, E, R>(self: Stream<A, E, R>) => Stream<A | B, E | E2, R | R2>
E2, function (type parameter) R in <A, E, R>(self: Stream<A, E, R>): Stream<A | B, E | E2, R | R2>R | function (type parameter) R2 in <B, E2, R2>(options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): <A, E, R>(self: Stream<A, E, R>) => Stream<A | B, E | E2, R | R2>
R2>
<function (type parameter) A in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
A, function (type parameter) E in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E, function (type parameter) R in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R, function (type parameter) B in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
B, function (type parameter) E2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E2, function (type parameter) R2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R2>(
self: Stream<A, E, R>(parameter) self: {
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; <…;
}
self: 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 <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
A, function (type parameter) E in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E, function (type parameter) R in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R>,
options: {
readonly duration: Duration.Input
readonly orElse: () => Stream<B, E2, R2>
}
options: {
readonly duration: Duration.Inputduration: import DurationDuration.type Input =
| number
| bigint
| Duration.Duration
| readonly [seconds: number, nanos: number]
| `${number} nano`
| `${number} nanos`
| `${number} micro`
| `${number} micros`
| `${number} milli`
| `${number} millis`
| `${number} second`
| `${number} seconds`
| `${number} minute`
| `${number} minutes`
| `${number} hour`
| `${number} hours`
| `${number} day`
| `${number} days`
| `${number} week`
| `${number} weeks`
| "Infinity"
| "-Infinity"
| Duration.DurationObject
Valid input types that can be converted to a Duration.
When to use
Use when an API should accept any value that Effect can convert into a
Duration, including existing durations, millisecond numbers, nanosecond
bigints, high-resolution tuples, duration strings, infinity strings, or
duration objects.
Details
String inputs accept values like "10 seconds", "500 millis",
"Infinity", and "-Infinity". Finite fractional values that are
normalized to nanoseconds are rounded to the nearest nanosecond, with ties
away from zero.
Input
readonly orElse: () => Stream<B, E2, R2>orElse: () => 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) B in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
B, function (type parameter) E2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E2, function (type parameter) R2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R2>
}
): 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 <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
A | function (type parameter) B in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
B, function (type parameter) E in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E | function (type parameter) E2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E2, function (type parameter) R in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R | function (type parameter) R2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R2>
} = dual<(...args: Array<any>) => any, <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}) => Stream<A | B, E | E2, R | R2>>(arity: 2, body: <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}) => Stream<A | B, E | E2, R | R2>): ((...args: Array<any>) => any) & (<A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}) => Stream<A | B, E | E2, R | R2>) (+1 overload)
Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(
2,
<function (type parameter) A in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
A, function (type parameter) E in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E, function (type parameter) R in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R, function (type parameter) B in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
B, function (type parameter) E2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E2, function (type parameter) R2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R2>(
self: Stream<A, E, R>(parameter) self: {
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; <…;
}
self: 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 <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
A, function (type parameter) E in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E, function (type parameter) R in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R>,
options: {
readonly duration: Duration.Input
readonly orElse: () => Stream<B, E2, R2>
}
options: {
readonly duration: Duration.Inputduration: import DurationDuration.type Input =
| number
| bigint
| Duration.Duration
| readonly [seconds: number, nanos: number]
| `${number} nano`
| `${number} nanos`
| `${number} micro`
| `${number} micros`
| `${number} milli`
| `${number} millis`
| `${number} second`
| `${number} seconds`
| `${number} minute`
| `${number} minutes`
| `${number} hour`
| `${number} hours`
| `${number} day`
| `${number} days`
| `${number} week`
| `${number} weeks`
| "Infinity"
| "-Infinity"
| Duration.DurationObject
Valid input types that can be converted to a Duration.
When to use
Use when an API should accept any value that Effect can convert into a
Duration, including existing durations, millisecond numbers, nanosecond
bigints, high-resolution tuples, duration strings, infinity strings, or
duration objects.
Details
String inputs accept values like "10 seconds", "500 millis",
"Infinity", and "-Infinity". Finite fractional values that are
normalized to nanoseconds are rounded to the nearest nanosecond, with ties
away from zero.
Input
readonly orElse: () => Stream<B, E2, R2>orElse: () => 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) B in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
B, function (type parameter) E2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E2, function (type parameter) R2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R2>
}
): 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 <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
A | function (type parameter) B in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
B, function (type parameter) E in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E | function (type parameter) E2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E2, function (type parameter) R in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R | function (type parameter) R2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R2> => {
const const duration: Duration.Durationconst duration: {
value: DurationValue;
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;
}
duration = import DurationDuration.const fromInputUnsafe: (
input: Input
) => Duration
Decodes a Duration.Input into a Duration.
When to use
Use when the input has already been validated or comes from a trusted source
and throwing is acceptable for invalid duration syntax.
Gotchas
If the input is not a valid Duration.Input, it throws an error.
Example (Decoding duration inputs)
import { Duration } from "effect"
const duration1 = Duration.fromInputUnsafe(1000) // 1000 milliseconds
const duration2 = Duration.fromInputUnsafe("5 seconds")
const duration3 = Duration.fromInputUnsafe("Infinity")
const duration4 = Duration.fromInputUnsafe([2, 500_000_000]) // 2 seconds and 500ms
fromInputUnsafe(options: {
readonly duration: Duration.Input
readonly orElse: () => Stream<B, E2, R2>
}
options.duration: Duration.Inputduration)
if (!import DurationDuration.const isFinite: (
self: Duration
) => boolean
Checks whether a Duration is finite (not infinite).
Example (Checking finite durations)
import { Duration } from "effect"
console.log(Duration.isFinite(Duration.seconds(5))) // true
console.log(Duration.isFinite(Duration.infinity)) // false
isFinite(const duration: Duration.Durationconst duration: {
value: DurationValue;
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;
}
duration)) return self: Stream<A, E, R>(parameter) self: {
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; <…;
}
self
if (import DurationDuration.const isZero: (self: Duration) => booleanChecks whether a Duration is zero.
Example (Checking for zero durations)
import { Duration } from "effect"
console.log(Duration.isZero(Duration.zero)) // true
console.log(Duration.isZero(Duration.seconds(1))) // false
isZero(const duration: Duration.Durationconst duration: {
value: DurationValue;
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;
}
duration)) return const suspend: <A, E, R>(
stream: LazyArg<Stream<A, E, R>>
) => Stream<A, E, R>
Creates a lazily constructed stream.
Details
The stream factory is evaluated each time the stream is run.
Example (Creating a lazily constructed stream)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const values = yield* Stream.suspend(() => Stream.make(1, 2, 3)).pipe(Stream.runCollect)
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 1, 2, 3 ]
suspend(options: {
readonly duration: Duration.Input
readonly orElse: () => Stream<B, E2, R2>
}
options.orElse: () => Stream<B, E2, R2>orElse)
const const timeoutSymbol: unique symboltimeoutSymbol = var Symbol: SymbolConstructor
;(description?: string | number) => symbol
Returns a new unique Symbol value.
Symbol()
return const catchCause: {
<E, A2, E2, R2>(
f: (
cause: Cause.Cause<E>
) => Stream<A2, E2, R2>
): <A, R>(
self: Stream<A, E, R>
) => Stream<A | A2, E2, R2 | R>
<A, E, R, A2, E2, R2>(
self: Stream<A, E, R>,
f: (
cause: Cause.Cause<E>
) => Stream<A2, E2, R2>
): Stream<A | A2, E2, R | R2>
}
catchCause(
const suspend: <A, E, R>(
stream: LazyArg<Stream<A, E, R>>
) => Stream<A, E, R>
Creates a lazily constructed stream.
Details
The stream factory is evaluated each time the stream is run.
Example (Creating a lazily constructed stream)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const values = yield* Stream.suspend(() => Stream.make(1, 2, 3)).pipe(Stream.runCollect)
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 1, 2, 3 ]
suspend(() => {
const const parent: Fiber.Fiber<any, any>const parent: {
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; <…;
}
parent = import FiberFiber.const getCurrent: () =>
| Fiber<any, any>
| undefined
Returns the current fiber if called from within a fiber context,
otherwise returns undefined.
When to use
Use when you need low-level runtime integrations that need access to the currently
executing fiber.
Gotchas
This is a synchronous accessor, not an Effect. It returns undefined outside
an active fiber runtime context.
Example (Getting the current fiber)
import { Effect, Fiber } from "effect"
const program = Effect.gen(function*() {
const current = Fiber.getCurrent()
if (current) {
console.log(`Current fiber ID: ${current.id}`)
}
})
getCurrent()!
const const clock: Clockconst clock: {
currentTimeMillisUnsafe: () => number;
currentTimeMillis: Effect<number>;
currentTimeNanosUnsafe: () => bigint;
currentTimeNanos: Effect<bigint>;
sleep: (duration: Duration.Duration) => Effect<void>;
}
clock = const parent: Fiber.Fiber<any, any>const parent: {
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; <…;
}
parent.Fiber<any, any>.getRef: <A>(ref: Context.Reference<A>) => AgetRef(const Clock: Context.Reference<Clock>(alias) const Clock: {
key: string;
Service: {
currentTimeMillisUnsafe: () => number;
currentTimeMillis: Effect<number>;
currentTimeNanosUnsafe: () => bigint;
currentTimeNanos: Effect<bigint>;
sleep: (duration: Duration.Duration) => Effect<void>;
};
defaultValue: () => Shape;
of: (this: void, self: Clock) => Clock;
context: (self: Clock) => Context.Context<never>;
use: (f: (service: Clock) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
useSync: (f: (service: Clock) => A) => Effect.Effect<A, never, never>;
Identifier: Identifier;
stack: string | 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; <…;
toString: () => string;
toJSON: () => unknown;
}
Represents a time-based clock which provides functionality related to time
and scheduling.
When to use
Use to define or provide a clock service for current-time and sleep
operations.
Example (Reading current time)
import { Clock, Effect } from "effect"
const clockOperations = Effect.gen(function*() {
const currentTime = yield* Clock.currentTimeMillis
const currentTimeNanos = yield* Clock.currentTimeNanos
console.log(`Current time (ms): ${currentTime}`)
console.log(`Current time (ns): ${currentTimeNanos}`)
})
Context reference for the active time service in the environment.
When to use
Use when you need to access or provide the full time service, including sleep
operations, rather than a single timestamp accessor.
Example (Accessing the Clock service)
import { Clock, Effect } from "effect"
const program = Effect.gen(function*() {
const clock = yield* Clock.Clock
return clock.currentTimeMillisUnsafe()
})
Clock)
const const durationMs: numberdurationMs = import DurationDuration.const toMillis: (self: Input) => numberConverts a Duration to milliseconds.
Example (Converting durations to milliseconds)
import { Duration } from "effect"
console.log(Duration.toMillis(Duration.seconds(5))) // 5000
console.log(Duration.toMillis(Duration.minutes(2))) // 120000
toMillis(const duration: Duration.Durationconst duration: {
value: DurationValue;
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;
}
duration)
let let deadline: number | undefineddeadline: number | undefined = var undefinedundefined
const const latch: Latch.Latchconst latch: {
open: Effect.Effect<boolean>;
openUnsafe: (this: Latch) => boolean;
release: Effect.Effect<boolean>;
await: Effect.Effect<void>;
close: Effect.Effect<boolean>;
closeUnsafe: (this: Latch) => boolean;
whenOpen: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
isOpen: (this: Latch) => boolean;
}
latch = import LatchLatch.const makeUnsafe: (
open?: boolean | undefined
) => Latch
Creates a Latch synchronously, outside of Effect.
When to use
Use when you need to allocate a Latch synchronously outside an Effect
workflow.
Details
The latch starts closed by default; pass true to create it open.
Example (Creating a latch unsafely)
import { Effect, Latch } from "effect"
const latch = Latch.makeUnsafe(false)
const waiter = Effect.gen(function*() {
yield* Effect.log("Waiting for latch to open...")
yield* latch.await
yield* Effect.log("Latch opened! Continuing...")
})
const opener = Effect.gen(function*() {
yield* Effect.sleep("2 seconds")
yield* Effect.log("Opening latch...")
yield* latch.open
})
const program = Effect.all([waiter, opener])
makeUnsafe(false)
return const merge: {
<A2, E2, R2>(
that: Stream<A2, E2, R2>,
options?:
| {
readonly haltStrategy?:
| HaltStrategy
| undefined
}
| undefined
): <A, E, R>(
self: Stream<A, E, R>
) => Stream<A | A2, E | E2, R | R2>
<A, E, R, A2, E2, R2>(
self: Stream<A, E, R>,
that: Stream<A2, E2, R2>,
options?:
| {
readonly haltStrategy?:
| HaltStrategy
| undefined
}
| undefined
): Stream<A | A2, E | E2, R | R2>
}
merge(
const transformPull: <
A,
E,
R,
B,
E2,
R2,
EX,
RX
>(
self: Stream<A, E, R>,
f: (
pull: Pull.Pull<
Arr.NonEmptyReadonlyArray<A>,
E,
void
>,
scope: Scope.Scope
) => Effect.Effect<
Pull.Pull<
Arr.NonEmptyReadonlyArray<B>,
E2,
void,
R2
>,
EX,
RX
>
) => Stream<
B,
EX | Pull.ExcludeDone<E2>,
R | R2 | RX
>
Derives a stream by transforming its pull effect.
Example (Transforming a pull effect)
import { Console, Effect, Stream } from "effect"
const stream = Stream.make(1, 2, 3)
const transformed = Stream.transformPull(stream, (pull) => Effect.succeed(pull))
const program = Effect.gen(function*() {
const values = yield* Stream.runCollect(transformed)
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 1, 2, 3 ]
transformPull(self: Stream<A, E, R>(parameter) self: {
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; <…;
}
self, (pull: Pull.Pull<
readonly [A, ...A[]],
E,
void,
never
>
(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, _scope: Scope.Scope(parameter) _scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
_scope) =>
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 deadline: number | undefineddeadline = const clock: Clockconst clock: {
currentTimeMillisUnsafe: () => number;
currentTimeMillis: Effect<number>;
currentTimeNanosUnsafe: () => bigint;
currentTimeNanos: Effect<bigint>;
sleep: (duration: Duration.Duration) => Effect<void>;
}
clock.Clock.currentTimeMillisUnsafe(): numberReturns the current time in milliseconds unsafely.
When to use
Use to read millisecond time synchronously when you already have a Clock
service and can accept non-effectful access.
currentTimeMillisUnsafe() + const durationMs: numberdurationMs
const latch: Latch.Latchconst latch: {
open: Effect.Effect<boolean>;
openUnsafe: (this: Latch) => boolean;
release: Effect.Effect<boolean>;
await: Effect.Effect<void>;
close: Effect.Effect<boolean>;
closeUnsafe: (this: Latch) => boolean;
whenOpen: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
isOpen: (this: Latch) => boolean;
}
latch.Latch.openUnsafe(this: Latch): booleanOpens the latch synchronously, releasing all fibers waiting on it.
When to use
Use when synchronous code must open the latch immediately.
openUnsafe()
return pull: Pull.Pull<
readonly [A, ...A[]],
E,
void,
never
>
(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
}).Pipeable.pipe<Effect.Effect<readonly [A, ...A[]], Cause.Done<void> | E, never>, Effect.Effect<readonly [A, ...A[]], Cause.Done<void> | E, never>, Effect.Effect<Effect.Effect<readonly [A, ...A[]], Cause.Done<void> | E, never>, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<readonly [A, ...A[]], Cause.Done<void> | E, never>) => Effect.Effect<readonly [A, ...A[]], Cause.Done<void> | E, never>, bc: (_: Effect.Effect<readonly [A, ...A[]], Cause.Done<void> | E, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)pipe(
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((arr: readonly [A, ...A[]](parameter) arr: {
0: A;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<A>>): Array<A>; (...items: Array<A | ConcatArray<A>>): Array<A> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<A>;
indexOf: (searchElement: A, fromIndex?: number) => number;
lastIndexOf: (searchElement: A, fromIndex?: number) => number;
every: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: A, index: number, array: ReadonlyArray<A>) => void, thisArg?: any) => void;
map: (callbackfn: (value: A, index: number, array: ReadonlyArray<A>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): Array<S>; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): Array<A> };
reduce: { (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A): A; (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A, initialValue: A): A; (callbac…;
reduceRight: { (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A): A; (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A, initialValue: A): A; (callbac…;
find: { (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => unknown, thisArg?: any): A | undefined };
findIndex: (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, A]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<A>;
includes: (searchElement: A, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: A, index: number, array: Array<A>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => A | undefined;
findLast: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): A | undefined };
findLastIndex: (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any) => number;
toReversed: () => Array<A>;
toSorted: (compareFn?: ((a: A, b: A) => number) | undefined) => Array<A>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<A>): Array<A>; (start: number, deleteCount?: number): Array<A> };
with: (index: number, value: A) => Array<A>;
}
arr) => {
const latch: Latch.Latchconst latch: {
open: Effect.Effect<boolean>;
openUnsafe: (this: Latch) => boolean;
release: Effect.Effect<boolean>;
await: Effect.Effect<void>;
close: Effect.Effect<boolean>;
closeUnsafe: (this: Latch) => boolean;
whenOpen: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
isOpen: (this: Latch) => boolean;
}
latch.Latch.closeUnsafe(this: Latch): booleanCloses the latch synchronously so future waiters suspend again.
When to use
Use when synchronous code must close the latch immediately.
closeUnsafe()
let deadline: number | undefineddeadline = var undefinedundefined
return arr: readonly [A, ...A[]](parameter) arr: {
0: A;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<A>>): Array<A>; (...items: Array<A | ConcatArray<A>>): Array<A> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<A>;
indexOf: (searchElement: A, fromIndex?: number) => number;
lastIndexOf: (searchElement: A, fromIndex?: number) => number;
every: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: A, index: number, array: ReadonlyArray<A>) => void, thisArg?: any) => void;
map: (callbackfn: (value: A, index: number, array: ReadonlyArray<A>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): Array<S>; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): Array<A> };
reduce: { (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A): A; (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A, initialValue: A): A; (callbac…;
reduceRight: { (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A): A; (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A, initialValue: A): A; (callbac…;
find: { (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => unknown, thisArg?: any): A | undefined };
findIndex: (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, A]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<A>;
includes: (searchElement: A, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: A, index: number, array: Array<A>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => A | undefined;
findLast: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): A | undefined };
findLastIndex: (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any) => number;
toReversed: () => Array<A>;
toSorted: (compareFn?: ((a: A, b: A) => number) | undefined) => Array<A>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<A>): Array<A>; (start: number, deleteCount?: number): Array<A> };
with: (index: number, value: A) => Array<A>;
}
arr
}),
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
)),
const fromEffectDrain: <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Stream<never, E, R>
Creates a stream that runs the effect and emits no elements.
Example (Draining an effect into a stream)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
yield* Stream.fromEffectDrain(Console.log("Draining side effect")).pipe(
Stream.runDrain
)
})
Effect.runPromise(program)
// Output: Draining side effect
fromEffectDrain(import EffectEffect.const gen: {
<Eff extends Effect<any, any, any>, AEff>(
f: () => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer _E, infer R>
]
? R
: never
>
<Self, Eff extends Effect<any, any, any>, AEff>(
options: { readonly self: Self },
f: (this: Self) => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer _E, infer R>
]
? R
: never
>
}
gen(function*() {
while (true) {
yield* const latch: Latch.Latchconst latch: {
open: Effect.Effect<boolean>;
openUnsafe: (this: Latch) => boolean;
release: Effect.Effect<boolean>;
await: Effect.Effect<void>;
close: Effect.Effect<boolean>;
closeUnsafe: (this: Latch) => boolean;
whenOpen: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
isOpen: (this: Latch) => boolean;
}
latch.Latch.await: Effect.Effect<void>(property) Latch.await: {
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;
}
Waits for the latch to be opened or released.
When to use
Use to suspend until the latch allows the current fiber to continue.
await
if (let deadline: number | undefineddeadline === var undefinedundefined) continue
yield* import EffectEffect.const sleep: (
duration: Duration.Input
) => Effect<void>
Returns an effect that suspends the current fiber for the specified duration
without blocking a JavaScript thread.
Example (Pausing without blocking)
import { Console, Effect } from "effect"
const program = Effect.gen(function*() {
yield* Console.log("Start")
yield* Effect.sleep("2 seconds")
yield* Console.log("End")
})
Effect.runFork(program)
// Output: "Start" (immediately)
// Output: "End" (after 2 seconds)
sleep(let deadline: numberdeadline - const clock: Clockconst clock: {
currentTimeMillisUnsafe: () => number;
currentTimeMillis: Effect<number>;
currentTimeNanosUnsafe: () => bigint;
currentTimeNanos: Effect<bigint>;
sleep: (duration: Duration.Duration) => Effect<void>;
}
clock.Clock.currentTimeMillisUnsafe(): numberReturns the current time in milliseconds unsafely.
When to use
Use to read millisecond time synchronously when you already have a Clock
service and can accept non-effectful access.
currentTimeMillisUnsafe())
if (let deadline: numberdeadline === var undefinedundefined) continue
const const remaining: numberremaining = let deadline: numberdeadline - const clock: Clockconst clock: {
currentTimeMillisUnsafe: () => number;
currentTimeMillis: Effect<number>;
currentTimeNanosUnsafe: () => bigint;
currentTimeNanos: Effect<bigint>;
sleep: (duration: Duration.Duration) => Effect<void>;
}
clock.Clock.currentTimeMillisUnsafe(): numberReturns the current time in milliseconds unsafely.
When to use
Use to read millisecond time synchronously when you already have a Clock
service and can accept non-effectful access.
currentTimeMillisUnsafe()
if (const remaining: numberremaining > 0) continue
return yield* 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(const timeoutSymbol: unique symboltimeoutSymbol)
}
})),
{ haltStrategy?: Channel.HaltStrategy | undefinedhaltStrategy: "left" }
)
}),
(cause: Cause.Cause<Exclude<E, Cause.Done<any>>>(parameter) cause: {
reasons: ReadonlyArray<Reason<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;
}
cause): 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) B in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
B, function (type parameter) E in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E | function (type parameter) E2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E2, function (type parameter) R2 in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
R2> => {
const const isTimeout:
| Cause.Reason<Exclude<E, Cause.Done<any>>>
| undefined
isTimeout = cause: Cause.Cause<Exclude<E, Cause.Done<any>>>(parameter) cause: {
reasons: ReadonlyArray<Reason<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;
}
cause.Cause<Exclude<E, Done<any>>>.reasons: ReadonlyArray<Reason<E>>reasons.ReadonlyArray<Reason<Exclude<E, Done<any>>>>.find(predicate: (value: Cause.Reason<Exclude<E, Cause.Done<any>>>, index: number, obj: readonly Cause.Reason<Exclude<E, Cause.Done<any>>>[]) => unknown, thisArg?: any): Cause.Reason<Exclude<E, Cause.Done<any>>> | undefined (+1 overload)Returns the value of the first element in the array where predicate is true, and undefined
otherwise.
find((r: Cause.Reason<
Exclude<E, Cause.Done<any>>
>
r) => r: Cause.Reason<
Exclude<E, Cause.Done<any>>
>
r.Cause<out E>.ReasonProto<Tag extends string>._tag: "Die" | "Interrupt" | "Fail"_tag === "Die" && r: Cause.Die(parameter) r: {
defect: unknown;
_tag: Tag;
annotations: ReadonlyMap<string, unknown>;
annotate: (annotations: Context.Context<never> | ReadonlyMap<string, unknown>, options?: { readonly overwrite?: boolean | undefined }) => Cause.Die;
toString: () => string;
toJSON: () => unknown;
}
r.Die.defect: unknowndefect === const timeoutSymbol: unique symboltimeoutSymbol)
if (const isTimeout:
| Cause.Reason<Exclude<E, Cause.Done<any>>>
| undefined
isTimeout) return options: {
readonly duration: Duration.Input
readonly orElse: () => Stream<B, E2, R2>
}
options.orElse: () => Stream<B, E2, R2>orElse()
return const failCause: <E>(
cause: Cause.Cause<E>
) => Stream<never, E>
Creates a stream that fails with the specified Cause.
Example (Failing with a cause)
import { Cause, Console, Effect, Stream } from "effect"
const stream = Stream.failCause(Cause.fail("Database connection failed")).pipe(
Stream.catchCause(() => Stream.succeed("recovered"))
)
const program = Effect.gen(function*() {
const values = yield* Stream.runCollect(stream)
yield* Console.log(values)
// Output: [ "recovered" ]
})
Effect.runPromise(program)
failCause(cause: Cause.Cause<Exclude<E, Cause.Done<any>>>(parameter) cause: {
reasons: ReadonlyArray<Reason<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;
}
cause as import CauseCause.interface Cause<out E>A structured representation of how an Effect failed.
When to use
Use to preserve the full structured failure information for an effect instead
of collapsing it to a single error value.
Details
Access the individual failure entries through the reasons array, then
narrow each entry with
isFailReason
,
isDieReason
, or
- Use
hasFails
/
hasDies
/
hasInterrupts
to test
for the presence of specific reason kinds without iterating.
- Use
findError
/
findDefect
to extract the first value
of a given kind.
- Use
combine
to merge two causes.
Cause implements Equal — two causes with the same reasons (by value)
compare as equal.
Example (Creating and inspecting a cause)
import { Cause } from "effect"
const cause = Cause.fail("Something went wrong")
console.log(cause.reasons.length) // 1
console.log(Cause.isFailReason(cause.reasons[0])) // true
Companion namespace for the Cause interface.
Cause<function (type parameter) E in <A, E, R, B, E2, R2>(self: Stream<A, E, R>, options: {
readonly duration: Duration.Input;
readonly orElse: () => Stream<B, E2, R2>;
}): Stream<A | B, E | E2, R | R2>
E>)
}
)
}
)