Resourceful Streams
The Stream module provides tools for working with resources that require explicit management. With functions like Stream.scoped
and Stream.acquireRelease
, you can create streams that handle resource acquisition and release automatically. Additionally, the Stream.finalizer
and Stream.ensuring
operators allow you to add final cleanup steps, ensuring any necessary post-stream actions are executed.
Stream.scoped
is used to create a single-valued stream from a scoped resource. It can be handy when dealing with resources that require explicit acquisition, usage, and release.
Example (Creating a Scoped Stream)
1import { import Stream
Stream, import Effect
Effect, import Console
Console } from "effect"2
3// Creating a single-valued stream from a scoped resource4const const stream: Stream.Stream<void, never, never>
stream = import Stream
Stream.const scoped: <void, never, never>(effect: Effect.Effect<void, never, never>) => Stream.Stream<void, never, never>
Creates a single-valued stream from a scoped resource.
scoped(5 import Effect
Effect.const acquireUseRelease: <void, never, never, void, never, never, void, never>(acquire: Effect.Effect<void, never, never>, use: (a: void) => Effect.Effect<void, never, never>, release: (a: void, exit: Exit<void, never>) => Effect.Effect<...>) => Effect.Effect<...> (+1 overload)
This function is used to ensure that an `Effect` value that represents the
acquisition of a resource (for example, opening a file, launching a thread,
etc.) will not be interrupted, and that the resource will always be released
when the `Effect` value completes execution.
`acquireUseRelease` does the following:
1. Ensures that the `Effect` value that acquires the resource will not be
interrupted. Note that acquisition may still fail due to internal
reasons (such as an uncaught exception).
2. Ensures that the `release` `Effect` value will not be interrupted,
and will be executed as long as the acquisition `Effect` value
successfully acquires the resource.
During the time period between the acquisition and release of the resource,
the `use` `Effect` value will be executed.
If the `release` `Effect` value fails, then the entire `Effect` value will
fail, even if the `use` `Effect` value succeeds. If this fail-fast behavior
is not desired, errors produced by the `release` `Effect` value can be caught
and ignored.
acquireUseRelease(6 import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log("acquire"), // Resource acquisition7 () => import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log("use"), // Resource usage within stream8 () => import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log("release") // Resource release after stream9 )10)11
12import Effect
Effect.const runPromise: <Chunk<void>, never>(effect: Effect.Effect<Chunk<void>, never, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<Chunk<void>>
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: <void, never, never>(self: Stream.Stream<void, never, never>) => Effect.Effect<Chunk<void>, never, never>
Runs the stream and collects all of its elements to a chunk.
runCollect(const stream: Stream.Stream<void, never, never>
stream)).(method) Promise<Chunk<void>>.then<void, never>(onfulfilled?: ((value: Chunk<void>) => 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)13/*14Output:15acquire16use17release18{ _id: 'Chunk', values: [ undefined ] }19*/
Stream.acquireRelease
is used to manage resources within a stream, ensuring safe acquisition and release. This is particularly useful when working with resources that require explicit opening and closing, such as file handling. Stream.acquireRelease
ensures that resources are acquired at the start and released after the stream completes, even if an error occurs.
Example (Acquiring and Releasing a File Resource)
In this example, we simulate file operations with an open
function. The Stream.acquireRelease
function guarantees that the file is properly opened and closed, and we process its contents while the file is open.
1import { import Stream
Stream, import Console
Console, import Effect
Effect } from "effect"2
3// Simulate file operations4const const open: (filename: string) => Effect.Effect<{
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<void, never, never>;
}, never, never>
open = ((parameter) filename: string
filename: string) =>5 import Effect
Effect.const gen: <never, {
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<void, never, never>;
}>(f: (resume: Effect.Adapter) => Generator<never, {
getLines: Effect.Effect<...>;
close: Effect.Effect<...>;
}, never>) => Effect.Effect<...> (+1 overload)
gen(function* () {6 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(`Opening ${(parameter) filename: string
filename}`)7 return {8 (property) getLines: Effect.Effect<string[], never, never>
getLines: import Effect
Effect.const succeed: <string[]>(value: string[]) => Effect.Effect<string[], never, never>
Creates an `Effect` that succeeds with the provided value.
Use this function to represent a successful computation that yields a value of type `A`.
The effect does not fail and does not require any environmental context.
succeed(["Line 1", "Line 2", "Line 3"]),9 (property) close: Effect.Effect<void, never, never>
close: import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log(`Closing ${(parameter) filename: string
filename}`)10 }11 })12
13const const stream: Stream.Stream<string[], never, never>
stream = import Stream
Stream.const acquireRelease: <{
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<void, never, never>;
}, never, never, never, void>(acquire: Effect.Effect<{
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<...>;
}, never, never>, release: (resource: {
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<...>;
}, exit: Exit<unknown, unknown>) => Effect.Effect<...>) => Stream.Stream<...>
Creates a stream from a single value that will get cleaned up after the
stream is consumed.
acquireRelease(14 const open: (filename: string) => Effect.Effect<{
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<void, never, never>;
}, never, never>
open("file.txt"),15 ((parameter) file: {
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<void, never, never>;
}
file) => (parameter) file: {
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<void, never, never>;
}
file.(property) close: Effect.Effect<void, never, never>
close // Ensure file is closed after stream completes16).(method) Pipeable.pipe<Stream.Stream<{
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<void, never, never>;
}, never, never>, Stream.Stream<string[], never, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+21 overloads)
pipe(17 import Stream
Stream.const flatMap: <{
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<void, never, never>;
}, string[], never, never>(f: (a: {
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<...>;
}) => Stream.Stream<...>, options?: {
readonly concurrency?: number | "unbounded" | undefined;
readonly bufferSize?: number | undefined;
readonly switch?: boolean | undefined;
} | undefined) => <E, R>(self: Stream.Stream<...>) => Stream.Stream<...> (+1 overload)
Returns a stream made of the concatenation in strict order of all the
streams produced by passing each element of this stream to `f0`
flatMap(18 ((parameter) file: {
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<void, never, never>;
}
file) => (parameter) file: {
getLines: Effect.Effect<string[], never, never>;
close: Effect.Effect<void, never, never>;
}
file.(property) getLines: Effect.Effect<string[], never, never>
getLines // Use the acquired resource19 )20)21
22import Effect
Effect.const runPromise: <Chunk<string[]>, never>(effect: Effect.Effect<Chunk<string[]>, never, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<Chunk<string[]>>
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[], never, never>(self: Stream.Stream<string[], never, never>) => Effect.Effect<Chunk<string[]>, never, never>
Runs the stream and collects all of its elements to a chunk.
runCollect(const stream: Stream.Stream<string[], never, never>
stream)).(method) Promise<Chunk<string[]>>.then<void, never>(onfulfilled?: ((value: Chunk<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) 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)23/*24Output:25Opening file.txt26Closing file.txt27{ _id: 'Chunk', values: [ [ 'Line 1', 'Line 2', 'Line 3' ] ] }28*/
The Stream.finalizer
function creates a single-element stream that never fails and executes the finalizer when it ends.
Example (Executing a Finalizer After Stream Completion)
1import { import Stream
Stream, import Console
Console, import Effect
Effect } from "effect"2
3const const program: Stream.Stream<void, never, never>
program = import Stream
Stream.const fromEffect: <void, never, never>(effect: Effect.Effect<void, never, never>) => Stream.Stream<void, never, never>
Either emits the success value of this effect or terminates the stream
with the failure value of this effect.
fromEffect(import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log("Application Logic.")).(method) Pipeable.pipe<Stream.Stream<void, never, never>, Stream.Stream<void, never, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<void, never, never>) => Stream.Stream<void, never, never>): Stream.Stream<...> (+21 overloads)
pipe(4 import Stream
Stream.const concat: <void, never, never>(that: Stream.Stream<void, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<void | 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 finalizer: <never, void>(finalizer: Effect.Effect<void, never, never>) => Stream.Stream<void, never, never>
Creates a one-element stream that never fails and executes the finalizer
when it ends.
finalizer(import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log("Finalizing the stream")))5)6
7import Effect
Effect.const runPromise: <Chunk<void>, never>(effect: Effect.Effect<Chunk<void>, never, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<Chunk<void>>
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: <void, never, never>(self: Stream.Stream<void, never, never>) => Effect.Effect<Chunk<void>, never, never>
Runs the stream and collects all of its elements to a chunk.
runCollect(const program: Stream.Stream<void, never, never>
program)).(method) Promise<Chunk<void>>.then<void, never>(onfulfilled?: ((value: Chunk<void>) => 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)8/*9Output:10Application Logic.11Finalizing the stream12{ _id: 'Chunk', values: [ undefined, undefined ] }13*/
The Stream.ensuring
function allows for additional actions to be performed after a stream’s finalization. This is helpful for tasks you want to complete after the stream has fully shut down.
Example (Executing Post-Finalization Actions)
1import { import Stream
Stream, import Console
Console, import Effect
Effect } from "effect"2
3const const program: Stream.Stream<void, never, never>
program = import Stream
Stream.const fromEffect: <void, never, never>(effect: Effect.Effect<void, never, never>) => Stream.Stream<void, never, never>
Either emits the success value of this effect or terminates the stream
with the failure value of this effect.
fromEffect(import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log("Application Logic.")).(method) Pipeable.pipe<Stream.Stream<void, never, never>, Stream.Stream<void, never, never>, Stream.Stream<void, never, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<void, never, never>) => Stream.Stream<...>, bc: (_: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+21 overloads)
pipe(4 import Stream
Stream.const concat: <void, never, never>(that: Stream.Stream<void, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<void | 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 finalizer: <never, void>(finalizer: Effect.Effect<void, never, never>) => Stream.Stream<void, never, never>
Creates a one-element stream that never fails and executes the finalizer
when it ends.
finalizer(import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log("Finalizing the stream"))),5 import Stream
Stream.const ensuring: <void, never>(finalizer: Effect.Effect<void, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, E, R> (+1 overload)
Executes the provided finalizer after this stream's finalizers run.
ensuring(6 import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log("Doing some other works after stream's finalization")7 )8)9
10import Effect
Effect.const runPromise: <Chunk<void>, never>(effect: Effect.Effect<Chunk<void>, never, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<Chunk<void>>
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: <void, never, never>(self: Stream.Stream<void, never, never>) => Effect.Effect<Chunk<void>, never, never>
Runs the stream and collects all of its elements to a chunk.
runCollect(const program: Stream.Stream<void, never, never>
program)).(method) Promise<Chunk<void>>.then<void, never>(onfulfilled?: ((value: Chunk<void>) => 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)11/*12Output:13Application Logic.14Finalizing the stream15Doing some other works after stream's finalization16{ _id: 'Chunk', values: [ undefined, undefined ] }17*/
The effect passed to Stream.ensuring
will execute even if the stream encounters an error, ensuring that the post-finalization actions run reliably.
Example (Executing Post-Finalization Actions After an Error)
1import { import Stream
Stream, import Console
Console, import Effect
Effect } from "effect"2
3const const program: Stream.Stream<void, string, never>
program = import Stream
Stream.const fromEffect: <never, string, never>(effect: Effect.Effect<never, string, never>) => Stream.Stream<never, string, 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 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("Oh no!")).(method) Pipeable.pipe<Stream.Stream<never, string, never>, Stream.Stream<void, string, never>, Stream.Stream<void, string, never>>(this: Stream.Stream<...>, ab: (_: Stream.Stream<never, string, never>) => Stream.Stream<...>, bc: (_: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+21 overloads)
pipe(4 import Stream
Stream.const concat: <void, never, never>(that: Stream.Stream<void, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<void | 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 finalizer: <never, void>(finalizer: Effect.Effect<void, never, never>) => Stream.Stream<void, never, never>
Creates a one-element stream that never fails and executes the finalizer
when it ends.
finalizer(import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log("Finalizing the stream"))),5 import Stream
Stream.const ensuring: <void, never>(finalizer: Effect.Effect<void, never, never>) => <A, E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A, E, R> (+1 overload)
Executes the provided finalizer after this stream's finalizers run.
ensuring(6 import Console
Console.const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log("Doing some other works after stream's finalization")7 )8)9
10import Effect
Effect.const runPromise: <Chunk<void>, string>(effect: Effect.Effect<Chunk<void>, string, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<Chunk<void>>
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: <void, string, never>(self: Stream.Stream<void, string, never>) => Effect.Effect<Chunk<void>, string, never>
Runs the stream and collects all of its elements to a chunk.
runCollect(const program: Stream.Stream<void, string, never>
program))11/*12Output:13Doing some other works after stream's finalization14[Error: Oh no!] {15 name: '(FiberFailure) Error',16 [Symbol(effect/Runtime/FiberFailure/Cause)]: {17 _tag: 'Fail',18 error: 'Oh no!'19 }20 ...21}22*/