Skip to content

Error Handling in Streams

When working with streams that may encounter errors, it’s crucial to know how to handle these errors gracefully. The Stream.orElse function is a powerful tool for recovering from failures and switching to an alternative stream in case of an error.

Example

1
import {
import Stream
Stream
,
import Effect
Effect
} from "effect"
2
3
const
const s1: Stream.Stream<number, string, never>
s1
=
import Stream
Stream
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(1, 2, 3).
(method) Pipeable.pipe<Stream.Stream<number, never, never>, Stream.Stream<number, string, never>, Stream.Stream<number, string, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<number, never, never>) => Stream.Stream<...>, bc: (_: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const concat: <never, string, never>(that: Stream.Stream<never, string, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, string | E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const fail: <string>(error: string) => Stream.Stream<never, string, never>

Terminates with the specified error.

fail
("Oh! Error!")),
5
import Stream
Stream
.
const concat: <number, never, never>(that: Stream.Stream<number, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<number | A, E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const make: <[number, number]>(as_0: number, as_1: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(4, 5))
6
)
7
8
const
const s2: Stream.Stream<string, never, never>
s2
=
import Stream
Stream
.
const make: <[string, string, string]>(as_0: string, as_1: string, as_2: string) => Stream.Stream<string, never, never>

Creates a stream from an sequence of values.

make
("a", "b", "c")
9
10
const
const stream: Stream.Stream<string | number, never, never>
stream
=
import Stream
Stream
.
const orElse: <number, string, never, string, never, never>(self: Stream.Stream<number, string, never>, that: LazyArg<Stream.Stream<string, never, never>>) => Stream.Stream<string | number, never, never> (+1 overload)

Switches to the provided stream in case this one fails with a typed error. See also `Stream.catchAll`.

orElse
(
const s1: Stream.Stream<number, string, never>
s1
, () =>
const s2: Stream.Stream<string, never, never>
s2
)
11
12
import Effect
Effect
.
const runPromise: <Chunk<string | number>, never>(effect: Effect.Effect<Chunk<string | number>, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
import Stream
Stream
.
const runCollect: <string | number, never, never>(self: Stream.Stream<string | number, never, never>) => Effect.Effect<Chunk<string | number>, never, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<string | number, never, never>
stream
)).
(method) Promise<Chunk<string | number>>.then<void, never>(onfulfilled?: ((value: Chunk<string | number>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
13
/*
14
Output:
15
{
16
_id: "Chunk",
17
values: [ 1, 2, 3, "a", "b", "c" ]
18
}
19
*/

In this example, s1 encounters an error, but instead of terminating the stream, we gracefully switch to s2 using Stream.orElse. This ensures that we can continue processing data even if one stream fails.

There’s also a variant called Stream.orElseEither that uses the Either data type to distinguish elements from the two streams based on success or failure:

1
import {
import Stream
Stream
,
import Effect
Effect
} from "effect"
2
3
const
const s1: Stream.Stream<number, string, never>
s1
=
import Stream
Stream
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(1, 2, 3).
(method) Pipeable.pipe<Stream.Stream<number, never, never>, Stream.Stream<number, string, never>, Stream.Stream<number, string, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<number, never, never>) => Stream.Stream<...>, bc: (_: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const concat: <never, string, never>(that: Stream.Stream<never, string, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, string | E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const fail: <string>(error: string) => Stream.Stream<never, string, never>

Terminates with the specified error.

fail
("Oh! Error!")),
5
import Stream
Stream
.
const concat: <number, never, never>(that: Stream.Stream<number, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<number | A, E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const make: <[number, number]>(as_0: number, as_1: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(4, 5))
6
)
7
8
const
const s2: Stream.Stream<string, never, never>
s2
=
import Stream
Stream
.
const make: <[string, string, string]>(as_0: string, as_1: string, as_2: string) => Stream.Stream<string, never, never>

Creates a stream from an sequence of values.

make
("a", "b", "c")
9
10
const
const stream: Stream.Stream<Either<string, number>, never, never>
stream
=
import Stream
Stream
.
const orElseEither: <number, string, never, string, never, never>(self: Stream.Stream<number, string, never>, that: LazyArg<Stream.Stream<string, never, never>>) => Stream.Stream<Either<string, number>, never, never> (+1 overload)

Switches to the provided stream in case this one fails with a typed error. See also `Stream.catchAll`.

orElseEither
(
const s1: Stream.Stream<number, string, never>
s1
, () =>
const s2: Stream.Stream<string, never, never>
s2
)
11
12
import Effect
Effect
.
const runPromise: <Chunk<Either<string, number>>, never>(effect: Effect.Effect<Chunk<Either<string, number>>, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
import Stream
Stream
.
const runCollect: <Either<string, number>, never, never>(self: Stream.Stream<Either<string, number>, never, never>) => Effect.Effect<Chunk<Either<string, number>>, never, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<Either<string, number>, never, never>
stream
)).
(method) Promise<Chunk<Either<string, number>>>.then<void, never>(onfulfilled?: ((value: Chunk<Either<string, number>>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
13
/*
14
Output:
15
{
16
_id: "Chunk",
17
values: [
18
{
19
_id: "Either",
20
_tag: "Left",
21
left: 1
22
}, {
23
_id: "Either",
24
_tag: "Left",
25
left: 2
26
}, {
27
_id: "Either",
28
_tag: "Left",
29
left: 3
30
}, {
31
_id: "Either",
32
_tag: "Right",
33
right: "a"
34
}, {
35
_id: "Either",
36
_tag: "Right",
37
right: "b"
38
}, {
39
_id: "Either",
40
_tag: "Right",
41
right: "c"
42
}
43
]
44
}
45
*/

The Stream.catchAll function provides advanced error handling capabilities compared to Stream.orElse. With Stream.catchAll, you can make decisions based on both the type and value of the encountered failure.

1
import {
import Stream
Stream
,
import Effect
Effect
} from "effect"
2
3
const
const s1: Stream.Stream<number, "Uh Oh!" | "Ouch", never>
s1
=
import Stream
Stream
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(1, 2, 3).
(method) Pipeable.pipe<Stream.Stream<number, never, never>, Stream.Stream<number, "Uh Oh!", never>, Stream.Stream<number, "Uh Oh!", never>, Stream.Stream<number, "Uh Oh!" | "Ouch", never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<...>) => Stream.Stream<...>, bc: (_: Stream.Stream<...>) => Stream.Stream<...>, cd: (_: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const concat: <never, "Uh Oh!", never>(that: Stream.Stream<never, "Uh Oh!", never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, "Uh Oh!" | E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const fail: <"Uh Oh!">(error: "Uh Oh!") => Stream.Stream<never, "Uh Oh!", never>

Terminates with the specified error.

fail
("Uh Oh!" as
type const = "Uh Oh!"
const
)),
5
import Stream
Stream
.
const concat: <number, never, never>(that: Stream.Stream<number, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<number | A, E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const make: <[number, number]>(as_0: number, as_1: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(4, 5)),
6
import Stream
Stream
.
const concat: <never, "Ouch", never>(that: Stream.Stream<never, "Ouch", never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, "Ouch" | E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const fail: <"Ouch">(error: "Ouch") => Stream.Stream<never, "Ouch", never>

Terminates with the specified error.

fail
("Ouch" as
type const = "Ouch"
const
))
7
)
8
9
const
const s2: Stream.Stream<string, never, never>
s2
=
import Stream
Stream
.
const make: <[string, string, string]>(as_0: string, as_1: string, as_2: string) => Stream.Stream<string, never, never>

Creates a stream from an sequence of values.

make
("a", "b", "c")
10
11
const
const s3: Stream.Stream<boolean, never, never>
s3
=
import Stream
Stream
.
const make: <[boolean, boolean, boolean]>(as_0: boolean, as_1: boolean, as_2: boolean) => Stream.Stream<boolean, never, never>

Creates a stream from an sequence of values.

make
(true, false, false)
12
13
const
const stream: Stream.Stream<string | number | boolean, never, never>
stream
=
import Stream
Stream
.
const catchAll: <number, "Uh Oh!" | "Ouch", never, string | boolean, never, never>(self: Stream.Stream<number, "Uh Oh!" | "Ouch", never>, f: (error: "Uh Oh!" | "Ouch") => Stream.Stream<string | boolean, never, never>) => Stream.Stream<...> (+1 overload)

Switches over to the stream produced by the provided function in case this one fails with a typed error.

catchAll
(
14
const s1: Stream.Stream<number, "Uh Oh!" | "Ouch", never>
s1
,
15
(
(parameter) error: "Uh Oh!" | "Ouch"
error
):
import Stream
Stream
.
interface Stream<out A, out E = never, out R = never> namespace Stream

A `Stream<A, E, R>` is a description of a program that, when evaluated, may emit zero or more values of type `A`, may fail with errors of type `E`, and uses an context of type `R`. One way to think of `Stream` is as a `Effect` program that could emit multiple values. `Stream` is a purely functional *pull* based stream. Pull based streams offer inherent laziness and backpressure, relieving users of the need to manage buffers between operators. As an optimization, `Stream` does not emit single values, but rather an array of values. This allows the cost of effect evaluation to be amortized. `Stream` forms a monad on its `A` type parameter, and has error management facilities for its `E` type parameter, modeled similarly to `Effect` (with some adjustments for the multiple-valued nature of `Stream`). These aspects allow for rich and expressive composition of streams.

Stream
<string | boolean> => {
16
switch (
(parameter) error: "Uh Oh!" | "Ouch"
error
) {
17
case "Uh Oh!":
18
return
const s2: Stream.Stream<string, never, never>
s2
19
case "Ouch":
20
return
const s3: Stream.Stream<boolean, never, never>
s3
21
}
22
}
23
)
24
25
import Effect
Effect
.
const runPromise: <Chunk<string | number | boolean>, never>(effect: Effect.Effect<Chunk<string | number | boolean>, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
import Stream
Stream
.
const runCollect: <string | number | boolean, never, never>(self: Stream.Stream<string | number | boolean, never, never>) => Effect.Effect<Chunk<string | number | boolean>, never, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<string | number | boolean, never, never>
stream
)).
(method) Promise<Chunk<string | number | boolean>>.then<void, never>(onfulfilled?: ((value: Chunk<string | number | boolean>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
26
/*
27
Output:
28
{
29
_id: "Chunk",
30
values: [ 1, 2, 3, "a", "b", "c" ]
31
}
32
*/

In this example, we have a stream, s1, which may encounter two different types of errors. Instead of a straightforward switch to an alternative stream, as done with Stream.orElse, we employ Stream.catchAll to precisely determine how to handle each type of error. This level of control over error recovery enables you to choose different streams or actions based on the specific error conditions.

When working with streams, it’s essential to be prepared for various failure scenarios, including defects that might occur during stream processing. To address this, the Stream.catchAllCause function provides a robust solution. It enables you to gracefully handle and recover from any type of failure that may arise.

Example

1
import {
import Stream
Stream
,
import Effect
Effect
} from "effect"
2
3
const
const s1: Stream.Stream<number, never, never>
s1
=
import Stream
Stream
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(1, 2, 3).
(method) Pipeable.pipe<Stream.Stream<number, never, never>, Stream.Stream<number, never, never>, Stream.Stream<number, never, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<number, never, never>) => Stream.Stream<...>, bc: (_: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const concat: <never, never, never>(that: Stream.Stream<never, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const dieMessage: (message: string) => Stream.Stream<never>

The stream that dies with an exception described by `message`.

dieMessage
("Boom!")),
5
import Stream
Stream
.
const concat: <number, never, never>(that: Stream.Stream<number, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<number | A, E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const make: <[number, number]>(as_0: number, as_1: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(4, 5))
6
)
7
8
const
const s2: Stream.Stream<string, never, never>
s2
=
import Stream
Stream
.
const make: <[string, string, string]>(as_0: string, as_1: string, as_2: string) => Stream.Stream<string, never, never>

Creates a stream from an sequence of values.

make
("a", "b", "c")
9
10
const
const stream: Stream.Stream<string | number, never, never>
stream
=
import Stream
Stream
.
const catchAllCause: <number, never, never, string, never, never>(self: Stream.Stream<number, never, never>, f: (cause: Cause<never>) => Stream.Stream<string, never, never>) => Stream.Stream<...> (+1 overload)

Switches over to the stream produced by the provided function in case this one fails. Allows recovery from all causes of failure, including interruption if the stream is uninterruptible.

catchAllCause
(
const s1: Stream.Stream<number, never, never>
s1
, () =>
const s2: Stream.Stream<string, never, never>
s2
)
11
12
import Effect
Effect
.
const runPromise: <Chunk<string | number>, never>(effect: Effect.Effect<Chunk<string | number>, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
import Stream
Stream
.
const runCollect: <string | number, never, never>(self: Stream.Stream<string | number, never, never>) => Effect.Effect<Chunk<string | number>, never, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<string | number, never, never>
stream
)).
(method) Promise<Chunk<string | number>>.then<void, never>(onfulfilled?: ((value: Chunk<string | number>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
13
/*
14
Output:
15
{
16
_id: "Chunk",
17
values: [ 1, 2, 3, "a", "b", "c" ]
18
}
19
*/

In this example, s1 may encounter a defect, but instead of crashing the application, we use Stream.catchAllCause to gracefully switch to an alternative stream, s2. This ensures that your application remains robust and continues processing data even in the face of unexpected issues.

In stream processing, there may be situations where you need to recover from specific types of failures. The Stream.catchSome and Stream.catchSomeCause functions come to the rescue, allowing you to handle and mitigate errors selectively.

If you want to recover from a particular error, you can use Stream.catchSome:

1
import {
import Stream
Stream
,
import Effect
Effect
,
import Option
Option
} from "effect"
2
3
const
const s1: Stream.Stream<number, string, never>
s1
=
import Stream
Stream
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(1, 2, 3).
(method) Pipeable.pipe<Stream.Stream<number, never, never>, Stream.Stream<number, string, never>, Stream.Stream<number, string, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<number, never, never>) => Stream.Stream<...>, bc: (_: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const concat: <never, string, never>(that: Stream.Stream<never, string, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, string | E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const fail: <string>(error: string) => Stream.Stream<never, string, never>

Terminates with the specified error.

fail
("Oh! Error!")),
5
import Stream
Stream
.
const concat: <number, never, never>(that: Stream.Stream<number, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<number | A, E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const make: <[number, number]>(as_0: number, as_1: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(4, 5))
6
)
7
8
const
const s2: Stream.Stream<string, never, never>
s2
=
import Stream
Stream
.
const make: <[string, string, string]>(as_0: string, as_1: string, as_2: string) => Stream.Stream<string, never, never>

Creates a stream from an sequence of values.

make
("a", "b", "c")
9
10
const
const stream: Stream.Stream<string | number, string, never>
stream
=
import Stream
Stream
.
const catchSome: <number, string, never, string, never, never>(self: Stream.Stream<number, string, never>, pf: (error: string) => Option.Option<Stream.Stream<string, never, never>>) => Stream.Stream<...> (+1 overload)

Switches over to the stream produced by the provided function in case this one fails with some typed error.

catchSome
(
const s1: Stream.Stream<number, string, never>
s1
, (
(parameter) error: string
error
) => {
11
if (
(parameter) error: string
error
=== "Oh! Error!") {
12
return
import Option
Option
.
const some: <Stream.Stream<string, never, never>>(value: Stream.Stream<string, never, never>) => Option.Option<Stream.Stream<string, never, never>>

Creates a new `Option` that wraps the given value.

some
(
const s2: Stream.Stream<string, never, never>
s2
)
13
}
14
return
import Option
Option
.
const none: <never>() => Option.Option<never>

Creates a new `Option` that represents the absence of a value.

none
()
15
})
16
17
import Effect
Effect
.
const runPromise: <Chunk<string | number>, string>(effect: Effect.Effect<Chunk<string | number>, string, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
import Stream
Stream
.
const runCollect: <string | number, string, never>(self: Stream.Stream<string | number, string, never>) => Effect.Effect<Chunk<string | number>, string, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<string | number, string, never>
stream
)).
(method) Promise<Chunk<string | number>>.then<void, never>(onfulfilled?: ((value: Chunk<string | number>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
18
/*
19
Output:
20
{
21
_id: "Chunk",
22
values: [ 1, 2, 3, "a", "b", "c" ]
23
}
24
*/

To recover from a specific cause, you can use the Stream.catchSomeCause function:

1
import {
import Stream
Stream
,
import Effect
Effect
,
import Option
Option
,
import Cause
Cause
} from "effect"
2
3
const
const s1: Stream.Stream<number, never, never>
s1
=
import Stream
Stream
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(1, 2, 3).
(method) Pipeable.pipe<Stream.Stream<number, never, never>, Stream.Stream<number, never, never>, Stream.Stream<number, never, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<number, never, never>) => Stream.Stream<...>, bc: (_: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const concat: <never, never, never>(that: Stream.Stream<never, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const dieMessage: (message: string) => Stream.Stream<never>

The stream that dies with an exception described by `message`.

dieMessage
("Oh! Error!")),
5
import Stream
Stream
.
const concat: <number, never, never>(that: Stream.Stream<number, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<number | A, E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const make: <[number, number]>(as_0: number, as_1: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(4, 5))
6
)
7
8
const
const s2: Stream.Stream<string, never, never>
s2
=
import Stream
Stream
.
const make: <[string, string, string]>(as_0: string, as_1: string, as_2: string) => Stream.Stream<string, never, never>

Creates a stream from an sequence of values.

make
("a", "b", "c")
9
10
const
const stream: Stream.Stream<string | number, never, never>
stream
=
import Stream
Stream
.
const catchSomeCause: <number, never, never, string, never, never>(self: Stream.Stream<number, never, never>, pf: (cause: Cause.Cause<never>) => Option.Option<Stream.Stream<string, never, never>>) => Stream.Stream<...> (+1 overload)

Switches over to the stream produced by the provided function in case this one fails with some errors. Allows recovery from all causes of failure, including interruption if the stream is uninterruptible.

catchSomeCause
(
const s1: Stream.Stream<number, never, never>
s1
, (
(parameter) cause: Cause.Cause<never>
cause
) => {
11
if (
import Cause
Cause
.
const isDie: <never>(self: Cause.Cause<never>) => boolean

Returns `true` if the specified cause contains a defect, `false` otherwise.

isDie
(
(parameter) cause: Cause.Cause<never>
cause
)) {
12
return
import Option
Option
.
const some: <Stream.Stream<string, never, never>>(value: Stream.Stream<string, never, never>) => Option.Option<Stream.Stream<string, never, never>>

Creates a new `Option` that wraps the given value.

some
(
const s2: Stream.Stream<string, never, never>
s2
)
13
}
14
return
import Option
Option
.
const none: <never>() => Option.Option<never>

Creates a new `Option` that represents the absence of a value.

none
()
15
})
16
17
import Effect
Effect
.
const runPromise: <Chunk<string | number>, never>(effect: Effect.Effect<Chunk<string | number>, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
import Stream
Stream
.
const runCollect: <string | number, never, never>(self: Stream.Stream<string | number, never, never>) => Effect.Effect<Chunk<string | number>, never, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<string | number, never, never>
stream
)).
(method) Promise<Chunk<string | number>>.then<void, never>(onfulfilled?: ((value: Chunk<string | number>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
18
/*
19
Output:
20
{
21
_id: "Chunk",
22
values: [ 1, 2, 3, "a", "b", "c" ]
23
}
24
*/

In stream processing, it’s crucial to handle errors gracefully and perform cleanup tasks when needed. The Stream.onError function allows us to do just that. If our stream encounters an error, we can specify a cleanup task to be executed.

1
import {
import Stream
Stream
,
import Console
Console
,
import Effect
Effect
} from "effect"
2
3
const
const stream: Stream.Stream<number, never, never>
stream
=
import Stream
Stream
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(1, 2, 3).
(method) Pipeable.pipe<Stream.Stream<number, never, never>, Stream.Stream<number, never, never>, Stream.Stream<number, never, never>, Stream.Stream<number, never, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<...>) => Stream.Stream<...>, bc: (_: Stream.Stream<...>) => Stream.Stream<...>, cd: (_: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const concat: <never, never, never>(that: Stream.Stream<never, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const dieMessage: (message: string) => Stream.Stream<never>

The stream that dies with an exception described by `message`.

dieMessage
("Oh! Boom!")),
5
import Stream
Stream
.
const concat: <number, never, never>(that: Stream.Stream<number, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<number | A, E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
import Stream
Stream
.
const make: <[number, number]>(as_0: number, as_1: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(4, 5)),
6
import Stream
Stream
.
const onError: <never, void, never>(cleanup: (cause: Cause<never>) => Effect.Effect<void, never, never>) => <A, R>(self: Stream.Stream<A, never, R>) => Stream.Stream<A, never, R> (+1 overload)

Runs the specified effect if this stream fails, providing the error to the effect if it exists. Note: Unlike `Effect.onError` there is no guarantee that the provided effect will not be interrupted.

onError
(() =>
7
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
(
8
"Stream application closed! We are doing some cleanup jobs."
9
).
(method) Pipeable.pipe<Effect.Effect<void, never, never>, Effect.Effect<void, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<void, never, never>) => Effect.Effect<void, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const orDie: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, never, R>

Translates effect failure into death of the fiber, making all failures unchecked and not a part of the type of the effect.

orDie
)
10
)
11
)
12
13
import Effect
Effect
.
const runPromise: <Chunk<number>, never>(effect: Effect.Effect<Chunk<number>, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<Chunk<number>>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
import Stream
Stream
.
const runCollect: <number, never, never>(self: Stream.Stream<number, never, never>) => Effect.Effect<Chunk<number>, never, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<number, never, never>
stream
)).
(method) Promise<Chunk<number>>.then<void, never>(onfulfilled?: ((value: Chunk<number>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) globalThis.Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
14
/*
15
Output:
16
Stream application closed! We are doing some cleanup jobs.
17
error: RuntimeException: Oh! Boom!
18
*/

Sometimes, streams may encounter failures that are temporary or recoverable. In such cases, the Stream.retry operator comes in handy. It allows you to specify a retry schedule, and the stream will be retried according to that schedule.

Example

1
import {
import Stream
Stream
,
import Effect
Effect
,
import Schedule
Schedule
} from "effect"
2
import * as
(alias) module "node:readline" import NodeReadLine
NodeReadLine
from "node:readline"
3
4
const
const stream: Stream.Stream<number, string, never>
stream
=
import Stream
Stream
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(1, 2, 3).
(method) Pipeable.pipe<Stream.Stream<number, never, never>, Stream.Stream<number, string, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<number, never, never>) => Stream.Stream<number, string, never>): Stream.Stream<...> (+21 overloads)
pipe
(
5
import Stream
Stream
.
const concat: <number, string, never>(that: Stream.Stream<number, string, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<number | A, string | E, R> (+1 overload)

Concatenates the specified stream with this stream, resulting in a stream that emits the elements from this stream and then the elements from the specified stream.

concat
(
6
import Stream
Stream
.
const fromEffect: <number, string, never>(effect: Effect.Effect<number, string, never>) => Stream.Stream<number, string, never>

Either emits the success value of this effect or terminates the stream with the failure value of this effect.

fromEffect
(
7
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<never, string, never>> | YieldWrap<Effect.Effect<string, never, never>>, number>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
8
const
const s: string
s
= yield*
const readLine: (message: string) => Effect.Effect<string>
readLine
("Enter a number: ")
9
const
const n: number
n
=
function parseInt(string: string, radix?: number): number

Converts a string to an integer.

parseInt
(
const s: string
s
)
10
if (
var Number: NumberConstructor

An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.

Number
.
(method) NumberConstructor.isNaN(number: unknown): boolean

Returns a Boolean value that indicates whether a value is the reserved value NaN (not a number). Unlike the global isNaN(), Number.isNaN() doesn't forcefully convert the parameter to a number. Only values of the type number, that are also NaN, result in true.

isNaN
(
const n: number
n
)) {
11
return yield*
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>

Creates an `Effect` that represents a recoverable error. This `Effect` does not succeed but instead fails with the provided error. The failure can be of any type, and will propagate through the effect pipeline unless handled. Use this function when you want to explicitly signal an error in an `Effect` computation. The failed effect can later be handled with functions like {@link catchAll } or {@link catchTag } .

fail
("NaN")
12
}
13
return
const n: number
n
14
})
15
).
(method) Pipeable.pipe<Stream.Stream<number, string, never>, Stream.Stream<number, string, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<number, string, never>) => Stream.Stream<number, string, never>): Stream.Stream<...> (+21 overloads)
pipe
(
import Stream
Stream
.
const retry: <string, never, string, Duration>(schedule: Schedule.Schedule<Duration, string, never>) => <A, R>(self: Stream.Stream<A, string, R>) => Stream.Stream<...> (+1 overload)

When the stream fails, retry it according to the given schedule This retries the entire stream, so will re-execute all of the stream's acquire operations. The schedule is reset as soon as the first element passes through the stream again.

retry
(
import Schedule
Schedule
.
const exponential: (base: DurationInput, factor?: number) => Schedule.Schedule<Duration>

A schedule that always recurs, but will wait a certain amount between repetitions, given by `base * factor.pow(n)`, where `n` is the number of repetitions so far. Returns the current duration between recurrences.

exponential
("1 second")))
16
)
17
)
18
19
import Effect
Effect
.
const runPromise: <Chunk<number>, string>(effect: Effect.Effect<Chunk<number>, string, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<Chunk<number>>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
import Stream
Stream
.
const runCollect: <number, string, never>(self: Stream.Stream<number, string, never>) => Effect.Effect<Chunk<number>, string, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<number, string, never>
stream
)).
(method) Promise<Chunk<number>>.then<void, never>(onfulfilled?: ((value: Chunk<number>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
20
/*
21
Output:
22
Enter a number: a
23
Enter a number: b
24
Enter a number: c
25
Enter a number: 4
26
{
27
_id: "Chunk",
28
values: [ 1, 2, 3, 4 ]
29
}
30
*/
31
32
const
const readLine: (message: string) => Effect.Effect<string>
readLine
= (
(parameter) message: string
message
: string):
import Effect
Effect
.
interface Effect<out A, out E = never, out R = never> namespace Effect

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`. `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
<string> =>
33
import Effect
Effect
.
const promise: <string>(evaluate: (signal: AbortSignal) => PromiseLike<string>) => Effect.Effect<string, never, never>

Creates an `Effect` that represents an asynchronous computation guaranteed to succeed. The provided function (`thunk`) returns a `Promise` that should never reject. If the `Promise` does reject, the rejection is treated as a defect. An optional `AbortSignal` can be provided to allow for interruption of the wrapped `Promise` API.

promise
(
34
() =>
35
new
var Promise: PromiseConstructor new <string>(executor: (resolve: (value: string | PromiseLike<string>) => void, reject: (reason?: any) => void) => void) => Promise<string>

Creates a new Promise.

Promise
((
(parameter) resolve: (value: string | PromiseLike<string>) => void
resolve
) => {
36
const
const rl: NodeReadLine.Interface
rl
=
(alias) module "node:readline" import NodeReadLine
NodeReadLine
.
function createInterface(options: NodeReadLine.ReadLineOptions): NodeReadLine.Interface (+1 overload)

The `readline.createInterface()` method creates a new `readline.Interface` instance. ```js import readline from 'node:readline'; const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); ``` Once the `readline.Interface` instance is created, the most common case is to listen for the `'line'` event: ```js rl.on('line', (line) => { console.log(`Received: ${line}`); }); ``` If `terminal` is `true` for this instance then the `output` stream will get the best compatibility if it defines an `output.columns` property and emits a `'resize'` event on the `output` if or when the columns ever change (`process.stdout` does this automatically when it is a TTY). When creating a `readline.Interface` using `stdin` as input, the program will not terminate until it receives an [EOF character](https://en.wikipedia.org/wiki/End-of-file#EOF_character). To exit without waiting for user input, call `process.stdin.unref()`.

createInterface
({
37
(property) ReadLineOptions.input: NodeJS.ReadableStream

The [`Readable`](https://nodejs.org/docs/latest-v22.x/api/stream.html#readable-streams) stream to listen to

input
:
var process: NodeJS.Process
process
.
(property) NodeJS.Process.stdin: NodeJS.ReadStream & { fd: 0; }

The `process.stdin` property returns a stream connected to`stdin` (fd `0`). It is a `net.Socket` (which is a `Duplex` stream) unless fd `0` refers to a file, in which case it is a `Readable` stream. For details of how to read from `stdin` see `readable.read()`. As a `Duplex` stream, `process.stdin` can also be used in "old" mode that is compatible with scripts written for Node.js prior to v0.10\. For more information see `Stream compatibility`. In "old" streams mode the `stdin` stream is paused by default, so one must call `process.stdin.resume()` to read from it. Note also that calling `process.stdin.resume()` itself would switch stream to "old" mode.

stdin
,
38
(property) ReadLineOptions.output?: NodeJS.WritableStream | undefined

The [`Writable`](https://nodejs.org/docs/latest-v22.x/api/stream.html#writable-streams) stream to write readline data to.

output
:
var process: NodeJS.Process
process
.
(property) NodeJS.Process.stdout: NodeJS.WriteStream & { fd: 1; }

The `process.stdout` property returns a stream connected to`stdout` (fd `1`). It is a `net.Socket` (which is a `Duplex` stream) unless fd `1` refers to a file, in which case it is a `Writable` stream. For example, to copy `process.stdin` to `process.stdout`: ```js import { stdin, stdout } from 'node:process'; stdin.pipe(stdout); ``` `process.stdout` differs from other Node.js streams in important ways. See `note on process I/O` for more information.

stdout
39
})
40
const rl: NodeReadLine.Interface
rl
.
(method) Interface.question(query: string, callback: (answer: string) => void): void (+1 overload)

The `rl.question()` method displays the `query` by writing it to the `output`, waits for user input to be provided on `input`, then invokes the `callback` function passing the provided input as the first argument. When called, `rl.question()` will resume the `input` stream if it has been paused. If the `Interface` was created with `output` set to `null` or `undefined` the `query` is not written. The `callback` function passed to `rl.question()` does not follow the typical pattern of accepting an `Error` object or `null` as the first argument. The `callback` is called with the provided answer as the only argument. An error will be thrown if calling `rl.question()` after `rl.close()`. Example usage: ```js rl.question('What is your favorite food? ', (answer) => { console.log(`Oh, so your favorite food is ${answer}`); }); ``` Using an `AbortController` to cancel a question. ```js const ac = new AbortController(); const signal = ac.signal; rl.question('What is your favorite food? ', { signal }, (answer) => { console.log(`Oh, so your favorite food is ${answer}`); }); signal.addEventListener('abort', () => { console.log('The food question timed out'); }, { once: true }); setTimeout(() => ac.abort(), 10000); ```

question
(
(parameter) message: string
message
, (
(parameter) answer: string
answer
) => {
41
const rl: NodeReadLine.Interface
rl
.
(method) Interface.close(): void

The `rl.close()` method closes the `Interface` instance and relinquishes control over the `input` and `output` streams. When called, the `'close'` event will be emitted. Calling `rl.close()` does not immediately stop other events (including `'line'`) from being emitted by the `Interface` instance.

close
()
42
(parameter) resolve: (value: string | PromiseLike<string>) => void
resolve
(
(parameter) answer: string
answer
)
43
})
44
})
45
)

In this example, the stream asks the user to input a number, but if an invalid value is entered (e.g., “a,” “b,” “c”), it fails with “NaN.” However, we use Stream.retry with an exponential backoff schedule, which means it will retry after a delay of increasing duration. This allows us to handle temporary errors and eventually collect valid input.

When working with streams, there might be situations where you want to selectively keep certain errors and terminate the stream with the remaining errors. You can achieve this using the Stream.refineOrDie function.

Example

1
import {
import Stream
Stream
,
import Option
Option
} from "effect"
2
3
const
const stream: Stream.Stream<never, Error, never>
stream
=
import Stream
Stream
.
const fail: <Error>(error: Error) => Stream.Stream<never, Error, never>

Terminates with the specified error.

fail
(new
var Error: ErrorConstructor new (message?: string) => Error
Error
())
4
5
const
const res: Stream.Stream<never, SyntaxError, never>
res
=
import Stream
Stream
.
const refineOrDie: <never, Error, never, SyntaxError>(self: Stream.Stream<never, Error, never>, pf: (error: Error) => Option.Option<SyntaxError>) => Stream.Stream<...> (+1 overload)

Keeps some of the errors, and terminates the fiber with the rest

refineOrDie
(
const stream: Stream.Stream<never, Error, never>
stream
, (
(parameter) error: Error
error
) => {
6
if (
(parameter) error: Error
error
instanceof
var SyntaxError: SyntaxErrorConstructor
SyntaxError
) {
7
return
import Option
Option
.
const some: <SyntaxError>(value: SyntaxError) => Option.Option<SyntaxError>

Creates a new `Option` that wraps the given value.

some
(
(parameter) error: SyntaxError
error
)
8
}
9
return
import Option
Option
.
const none: <never>() => Option.Option<never>

Creates a new `Option` that represents the absence of a value.

none
()
10
})

In this example, stream initially fails with a generic Error. However, we use Stream.refineOrDie to filter and keep only errors of type SyntaxError. Any other errors will be terminated, while SyntaxErrors will be retained in refinedStream.

When working with streams, there are scenarios where you may want to handle timeouts, such as terminating a stream if it doesn’t produce a value within a certain duration. In this section, we’ll explore how to manage timeouts using various operators.

The Stream.timeout operator allows you to set a timeout on a stream. If the stream does not produce a value within the specified duration, it terminates.

1
import {
import Stream
Stream
,
import Effect
Effect
} from "effect"
2
3
const
const stream: Stream.Stream<never, never, never>
stream
=
import Stream
Stream
.
const fromEffect: <never, never, never>(effect: Effect.Effect<never, never, never>) => Stream.Stream<never, never, never>

Either emits the success value of this effect or terminates the stream with the failure value of this effect.

fromEffect
(
import Effect
Effect
.
const never: Effect.Effect<never, never, never>

Returns an effect that will never produce anything. The moral equivalent of `while(true) {}`, only without the wasted CPU cycles.

never
).
(method) Pipeable.pipe<Stream.Stream<never, never, never>, Stream.Stream<never, never, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<never, never, never>) => Stream.Stream<never, never, never>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const timeout: (duration: DurationInput) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, E, R> (+1 overload)

Ends the stream if it does not produce a value after the specified duration.

timeout
("2 seconds")
5
)
6
7
import Effect
Effect
.
const runPromise: <Chunk<never>, never>(effect: Effect.Effect<Chunk<never>, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<Chunk<never>>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
import Stream
Stream
.
const runCollect: <never, never, never>(self: Stream.Stream<never, never, never>) => Effect.Effect<Chunk<never>, never, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<never, never, never>
stream
)).
(method) Promise<Chunk<never>>.then<void, never>(onfulfilled?: ((value: Chunk<never>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
8
/*
9
{
10
_id: "Chunk",
11
values: []
12
}
13
*/

The Stream.timeoutFail operator combines a timeout with a custom failure message. If the stream times out, it fails with the specified error message.

1
import {
import Stream
Stream
,
import Effect
Effect
} from "effect"
2
3
const
const stream: Stream.Stream<never, string, never>
stream
=
import Stream
Stream
.
const fromEffect: <never, never, never>(effect: Effect.Effect<never, never, never>) => Stream.Stream<never, never, never>

Either emits the success value of this effect or terminates the stream with the failure value of this effect.

fromEffect
(
import Effect
Effect
.
const never: Effect.Effect<never, never, never>

Returns an effect that will never produce anything. The moral equivalent of `while(true) {}`, only without the wasted CPU cycles.

never
).
(method) Pipeable.pipe<Stream.Stream<never, never, never>, Stream.Stream<never, string, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<never, never, never>) => Stream.Stream<never, string, never>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const timeoutFail: <string>(error: LazyArg<string>, duration: DurationInput) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<...> (+1 overload)

Fails the stream with given error if it does not produce a value after d duration.

timeoutFail
(() => "timeout", "2 seconds")
5
)
6
7
import Effect
Effect
.
const runPromiseExit: <Chunk<never>, string>(effect: Effect.Effect<Chunk<never>, string, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<Exit<Chunk<never>, string>>

Executes an effect and returns a `Promise` that resolves with an `Exit` describing the result. Use `runPromiseExit` when you need detailed information about the outcome of the effect, including success or failure, and you want to work with Promises.

runPromiseExit
(
import Stream
Stream
.
const runCollect: <never, string, never>(self: Stream.Stream<never, string, never>) => Effect.Effect<Chunk<never>, string, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<never, string, never>
stream
)).
(method) Promise<Exit<Chunk<never>, string>>.then<void, never>(onfulfilled?: ((value: Exit<Chunk<never>, string>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
8
/*
9
Output:
10
{
11
_id: 'Exit',
12
_tag: 'Failure',
13
cause: { _id: 'Cause', _tag: 'Fail', failure: 'timeout' }
14
}
15
*/

Similar to Stream.timeoutFail, Stream.timeoutFailCause combines a timeout with a custom failure cause. If the stream times out, it fails with the specified cause.

1
import {
import Stream
Stream
,
import Effect
Effect
,
import Cause
Cause
} from "effect"
2
3
const
const stream: Stream.Stream<never, never, never>
stream
=
import Stream
Stream
.
const fromEffect: <never, never, never>(effect: Effect.Effect<never, never, never>) => Stream.Stream<never, never, never>

Either emits the success value of this effect or terminates the stream with the failure value of this effect.

fromEffect
(
import Effect
Effect
.
const never: Effect.Effect<never, never, never>

Returns an effect that will never produce anything. The moral equivalent of `while(true) {}`, only without the wasted CPU cycles.

never
).
(method) Pipeable.pipe<Stream.Stream<never, never, never>, Stream.Stream<never, never, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<never, never, never>) => Stream.Stream<never, never, never>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const timeoutFailCause: <never>(cause: LazyArg<Cause.Cause<never>>, duration: DurationInput) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<...> (+1 overload)

Fails the stream with given cause if it does not produce a value after d duration.

timeoutFailCause
(() =>
import Cause
Cause
.
const die: (defect: unknown) => Cause.Cause<never>

Constructs a new `Die` cause from the specified `defect`.

die
("timeout"), "2 seconds")
5
)
6
7
import Effect
Effect
.
const runPromiseExit: <Chunk<never>, never>(effect: Effect.Effect<Chunk<never>, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<Exit<Chunk<never>, never>>

Executes an effect and returns a `Promise` that resolves with an `Exit` describing the result. Use `runPromiseExit` when you need detailed information about the outcome of the effect, including success or failure, and you want to work with Promises.

runPromiseExit
(
import Stream
Stream
.
const runCollect: <never, never, never>(self: Stream.Stream<never, never, never>) => Effect.Effect<Chunk<never>, never, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<never, never, never>
stream
)).
(method) Promise<Exit<Chunk<never>, never>>.then<void, never>(onfulfilled?: ((value: Exit<Chunk<never>, never>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
8
/*
9
Output:
10
{
11
_id: 'Exit',
12
_tag: 'Failure',
13
cause: { _id: 'Cause', _tag: 'Die', defect: 'timeout' }
14
}
15
*/

The Stream.timeoutTo operator allows you to switch to another stream if the first stream does not produce a value within the specified duration.

1
import {
import Stream
Stream
,
import Effect
Effect
} from "effect"
2
3
const
const stream: Stream.Stream<number, never, never>
stream
=
import Stream
Stream
.
const fromEffect: <never, never, never>(effect: Effect.Effect<never, never, never>) => Stream.Stream<never, never, never>

Either emits the success value of this effect or terminates the stream with the failure value of this effect.

fromEffect
(
import Effect
Effect
.
const never: Effect.Effect<never, never, never>

Returns an effect that will never produce anything. The moral equivalent of `while(true) {}`, only without the wasted CPU cycles.

never
).
(method) Pipeable.pipe<Stream.Stream<never, never, never>, Stream.Stream<number, never, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<never, never, never>) => Stream.Stream<number, never, never>): Stream.Stream<...> (+21 overloads)
pipe
(
4
import Stream
Stream
.
const timeoutTo: <number, never, never>(duration: DurationInput, that: Stream.Stream<number, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<...> (+1 overload)

Switches the stream if it does not produce a value after the specified duration.

timeoutTo
("2 seconds",
import Stream
Stream
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Stream.Stream<number, never, never>

Creates a stream from an sequence of values.

make
(1, 2, 3))
5
)
6
7
import Effect
Effect
.
const runPromise: <Chunk<number>, never>(effect: Effect.Effect<Chunk<number>, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<Chunk<number>>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
import Stream
Stream
.
const runCollect: <number, never, never>(self: Stream.Stream<number, never, never>) => Effect.Effect<Chunk<number>, never, never>

Runs the stream and collects all of its elements to a chunk.

runCollect
(
const stream: Stream.Stream<number, never, never>
stream
)).
(method) Promise<Chunk<number>>.then<void, never>(onfulfilled?: ((value: Chunk<number>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
)
8
/*
9
{
10
_id: "Chunk",
11
values: [ 1, 2, 3 ]
12
}
13
*/