Skip to content

Parallel and Sequential Errors

In a typical Effect application, when an error occurs, it usually fails with the first error encountered by the Effect runtime:

1
import {
import Effect
Effect
} from "effect"
2
3
const
const fail: Effect.Effect<never, string, never>
fail
=
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>
fail
("Oh uh!")
4
const
const die: Effect.Effect<never, never, never>
die
=
import Effect
Effect
.
const dieMessage: (message: string) => Effect.Effect<never>

Returns an effect that dies with a `RuntimeException` having the specified text message. This method can be used for terminating a fiber because a defect has been detected in the code.

dieMessage
("Boom!")
5
6
const
const program: Effect.Effect<void, string, never>
program
=
import Effect
Effect
.
const all: <readonly [Effect.Effect<never, string, never>, Effect.Effect<never, never, never>], { readonly concurrency?: Concurrency | undefined; readonly batching?: boolean | "inherit" | undefined; readonly discard?: boolean | undefined; readonly mode?: "default" | "validate" | "either" | undefined; readonly concurrentFinalizers?: boolean | undefined; }>(arg: readonly [...], options?: { ...; } | undefined) => Effect.Effect<...>

Runs all the provided effects in sequence respecting the structure provided in input. Supports multiple arguments, a single argument tuple / array or record / struct.

all
([
const fail: Effect.Effect<never, string, never>
fail
,
const die: Effect.Effect<never, never, never>
die
]).
(method) Pipeable.pipe<Effect.Effect<[never, never], string, never>, Effect.Effect<never, string, never>, Effect.Effect<void, string, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<[never, never], string, never>) => Effect.Effect<...>, bc: (_: Effect.Effect<...>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
7
import Effect
Effect
.
const andThen: <Effect.Effect<never, never, never>>(f: Effect.Effect<never, never, never>) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<never, E, R> (+3 overloads)

Executes a sequence of two actions, typically two `Effect`s, where the second action can depend on the result of the first action. The `that` action can take various forms: - a value - a function returning a value - a promise - a function returning a promise - an effect - a function returning an effect

andThen
(
const die: Effect.Effect<never, never, never>
die
),
8
import Effect
Effect
.
const asVoid: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<void, E, R>

This function maps the success value of an `Effect` value to `void`. If the original `Effect` value succeeds, the returned `Effect` value will also succeed. If the original `Effect` value fails, the returned `Effect` value will fail with the same error.

asVoid
9
)
10
11
import Effect
Effect
.
const runPromiseExit: <void, string>(effect: Effect.Effect<void, string, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<Exit<void, string>>

Runs an `Effect` workflow, returning a `Promise` which resolves with the `Exit` value of the workflow.

runPromiseExit
(
const program: Effect.Effect<void, string, never>
program
).
(method) Promise<Exit<void, string>>.then<void, never>(onfulfilled?: ((value: Exit<void, 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
)
12
/*
13
Output:
14
{
15
_id: 'Exit',
16
_tag: 'Failure',
17
cause: { _id: 'Cause', _tag: 'Fail', failure: 'Oh uh!' }
18
}
19
*/

In this case, the program will fail with the first error, which is “Oh uh!”:

However, in some situations, you may encounter multiple errors, especially when performing parallel computations. When parallel computations are involved, the application may fail due to multiple errors:

1
import {
import Effect
Effect
} from "effect"
2
3
const
const fail: Effect.Effect<never, string, never>
fail
=
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>
fail
("Oh uh!")
4
const
const die: Effect.Effect<never, never, never>
die
=
import Effect
Effect
.
const dieMessage: (message: string) => Effect.Effect<never>

Returns an effect that dies with a `RuntimeException` having the specified text message. This method can be used for terminating a fiber because a defect has been detected in the code.

dieMessage
("Boom!")
5
6
const
const program: Effect.Effect<void, string, never>
program
=
import Effect
Effect
.
const all: <readonly [Effect.Effect<never, string, never>, Effect.Effect<never, never, never>], { concurrency: "unbounded"; }>(arg: readonly [Effect.Effect<never, string, never>, Effect.Effect<...>], options?: { ...; } | undefined) => Effect.Effect<...>

Runs all the provided effects in sequence respecting the structure provided in input. Supports multiple arguments, a single argument tuple / array or record / struct.

all
([
const fail: Effect.Effect<never, string, never>
fail
,
const die: Effect.Effect<never, never, never>
die
], {
7
(property) concurrency: "unbounded"
concurrency
: "unbounded"
8
}).
(method) Pipeable.pipe<Effect.Effect<[never, never], string, never>, Effect.Effect<void, string, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<[never, never], string, never>) => Effect.Effect<void, string, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const asVoid: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<void, E, R>

This function maps the success value of an `Effect` value to `void`. If the original `Effect` value succeeds, the returned `Effect` value will also succeed. If the original `Effect` value fails, the returned `Effect` value will fail with the same error.

asVoid
)
9
10
import Effect
Effect
.
const runPromiseExit: <void, string>(effect: Effect.Effect<void, string, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<Exit<void, string>>

Runs an `Effect` workflow, returning a `Promise` which resolves with the `Exit` value of the workflow.

runPromiseExit
(
const program: Effect.Effect<void, string, never>
program
).
(method) Promise<Exit<void, string>>.then<void, never>(onfulfilled?: ((value: Exit<void, 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
)
11
/*
12
Output:
13
{
14
_id: 'Exit',
15
_tag: 'Failure',
16
cause: {
17
_id: 'Cause',
18
_tag: 'Parallel',
19
left: { _id: 'Cause', _tag: 'Fail', failure: 'Oh uh!' },
20
right: { _id: 'Cause', _tag: 'Die', defect: [Object] }
21
}
22
}
23
*/

In this example, the program runs both fail and die concurrently, and if both fail, it will result in multiple errors.

Effect provides a useful function called Effect.parallelErrors that exposes all parallel failure errors in the error channel.

Example

1
import {
import Effect
Effect
} from "effect"
2
3
const
const fail1: Effect.Effect<never, string, never>
fail1
=
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>
fail
("Oh uh!")
4
const
const fail2: Effect.Effect<never, string, never>
fail2
=
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>
fail
("Oh no!")
5
const
const die: Effect.Effect<never, never, never>
die
=
import Effect
Effect
.
const dieMessage: (message: string) => Effect.Effect<never>

Returns an effect that dies with a `RuntimeException` having the specified text message. This method can be used for terminating a fiber because a defect has been detected in the code.

dieMessage
("Boom!")
6
7
const
const program: Effect.Effect<void, string[], never>
program
=
import Effect
Effect
.
const all: <readonly [Effect.Effect<never, string, never>, Effect.Effect<never, string, never>, Effect.Effect<never, never, never>], { concurrency: "unbounded"; }>(arg: readonly [...], options?: { ...; } | undefined) => Effect.Effect<...>

Runs all the provided effects in sequence respecting the structure provided in input. Supports multiple arguments, a single argument tuple / array or record / struct.

all
([
const fail1: Effect.Effect<never, string, never>
fail1
,
const fail2: Effect.Effect<never, string, never>
fail2
,
const die: Effect.Effect<never, never, never>
die
], {
8
(property) concurrency: "unbounded"
concurrency
: "unbounded"
9
}).
(method) Pipeable.pipe<Effect.Effect<[never, never, never], string, never>, Effect.Effect<void, string, never>, Effect.Effect<void, string[], never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<[never, never, never], string, never>) => Effect.Effect<...>, bc: (_: Effect.Effect<...>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const asVoid: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<void, E, R>

This function maps the success value of an `Effect` value to `void`. If the original `Effect` value succeeds, the returned `Effect` value will also succeed. If the original `Effect` value fails, the returned `Effect` value will fail with the same error.

asVoid
,
import Effect
Effect
.
const parallelErrors: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, Array<E>, R>

Exposes all parallel errors in a single call.

parallelErrors
)
10
11
import Effect
Effect
.
const runPromiseExit: <void, string[]>(effect: Effect.Effect<void, string[], never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<Exit<void, string[]>>

Runs an `Effect` workflow, returning a `Promise` which resolves with the `Exit` value of the workflow.

runPromiseExit
(
const program: Effect.Effect<void, string[], never>
program
).
(method) Promise<Exit<void, string[]>>.then<void, never>(onfulfilled?: ((value: Exit<void, 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
)
12
/*
13
Output:
14
{
15
_id: 'Exit',
16
_tag: 'Failure',
17
cause: { _id: 'Cause', _tag: 'Fail', failure: [ 'Oh uh!', 'Oh no!' ] }
18
}
19
*/

In this example, Effect.parallelErrors combines the errors from fail1 and fail2 into a single error.

When working with resource-safety operators like Effect.ensuring, you may encounter multiple sequential errors. This happens because regardless of whether the original effect has any errors or not, the finalizer is uninterruptible and will run:

1
import {
import Effect
Effect
} from "effect"
2
3
const
const fail: Effect.Effect<never, string, never>
fail
=
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>
fail
("Oh uh!")
4
const
const die: Effect.Effect<never, never, never>
die
=
import Effect
Effect
.
const dieMessage: (message: string) => Effect.Effect<never>

Returns an effect that dies with a `RuntimeException` having the specified text message. This method can be used for terminating a fiber because a defect has been detected in the code.

dieMessage
("Boom!")
5
6
const
const program: Effect.Effect<never, string, never>
program
=
const fail: Effect.Effect<never, string, never>
fail
.
(method) Pipeable.pipe<Effect.Effect<never, string, never>, Effect.Effect<never, string, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<never, string, never>) => Effect.Effect<never, string, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const ensuring: <never, never>(finalizer: Effect.Effect<never, never, never>) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R> (+1 overload)

Returns an effect that, if this effect _starts_ execution, then the specified `finalizer` is guaranteed to be executed, whether this effect succeeds, fails, or is interrupted. For use cases that need access to the effect's result, see `onExit`. Finalizers offer very powerful guarantees, but they are low-level, and should generally not be used for releasing resources. For higher-level logic built on `ensuring`, see the `acquireRelease` family of methods.

ensuring
(
const die: Effect.Effect<never, never, never>
die
))
7
8
import Effect
Effect
.
const runPromiseExit: <never, string>(effect: Effect.Effect<never, string, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<Exit<never, string>>

Runs an `Effect` workflow, returning a `Promise` which resolves with the `Exit` value of the workflow.

runPromiseExit
(
const program: Effect.Effect<never, string, never>
program
).
(method) Promise<Exit<never, string>>.then<void, never>(onfulfilled?: ((value: Exit<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
)
9
/*
10
Output:
11
{
12
_id: 'Exit',
13
_tag: 'Failure',
14
cause: {
15
_id: 'Cause',
16
_tag: 'Sequential',
17
left: { _id: 'Cause', _tag: 'Fail', failure: 'Oh uh!' },
18
right: { _id: 'Cause', _tag: 'Die', defect: [Object] }
19
}
20
}
21
*/

In this case, the program will result in multiple sequential errors if both fail and the finalizer die encounter errors.