Skip to content

Matching

In the Effect data type, just like other data types such as Option and Exit, we have a match function that allows us to handle different cases simultaneously. When working with effects, we also have several functions that enable us to handle both success and failure scenarios.

The Effect.match function allows us to handle both success and failure cases in a non-effectful manner by providing a handler for each case.

Example

1
import {
import Effect
Effect
} from "effect"
2
3
const
const success: Effect.Effect<number, Error, never>
success
:
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
<number,
interface Error
Error
> =
import Effect
Effect
.
const succeed: <number>(value: number) => Effect.Effect<number, never, never>
succeed
(42)
4
const
const failure: Effect.Effect<number, Error, never>
failure
:
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
<number,
interface Error
Error
> =
import Effect
Effect
.
const fail: <Error>(error: Error) => Effect.Effect<never, Error, never>
fail
(
5
new
var Error: ErrorConstructor new (message?: string) => Error
Error
("Uh oh!")
6
)
7
8
const
const program1: Effect.Effect<string, never, never>
program1
=
import Effect
Effect
.
const match: <number, Error, never, string, string>(self: Effect.Effect<number, Error, never>, options: { readonly onFailure: (error: Error) => string; readonly onSuccess: (value: number) => string; }) => Effect.Effect<...> (+1 overload)

Folds over the failure value or the success value to yield an effect that does not fail, but succeeds with the value returned by the left or right function passed to `match`.

match
(
const success: Effect.Effect<number, Error, never>
success
, {
9
(property) onFailure: (error: Error) => string
onFailure
: (
(parameter) error: Error
error
) => `failure: ${
(parameter) error: Error
error
.
(property) Error.message: string
message
}`,
10
(property) onSuccess: (value: number) => string
onSuccess
: (
(parameter) value: number
value
) => `success: ${
(parameter) value: number
value
}`
11
})
12
13
import Effect
Effect
.
const runPromise: <string, never>(effect: Effect.Effect<string, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<string>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
const program1: Effect.Effect<string, never, never>
program1
).
(method) Promise<string>.then<void, never>(onfulfilled?: ((value: 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
)
14
// Output: "success: 42"
15
16
const
const program2: Effect.Effect<string, never, never>
program2
=
import Effect
Effect
.
const match: <number, Error, never, string, string>(self: Effect.Effect<number, Error, never>, options: { readonly onFailure: (error: Error) => string; readonly onSuccess: (value: number) => string; }) => Effect.Effect<...> (+1 overload)

Folds over the failure value or the success value to yield an effect that does not fail, but succeeds with the value returned by the left or right function passed to `match`.

match
(
const failure: Effect.Effect<number, Error, never>
failure
, {
17
(property) onFailure: (error: Error) => string
onFailure
: (
(parameter) error: Error
error
) => `failure: ${
(parameter) error: Error
error
.
(property) Error.message: string
message
}`,
18
(property) onSuccess: (value: number) => string
onSuccess
: (
(parameter) value: number
value
) => `success: ${
(parameter) value: number
value
}`
19
})
20
21
import Effect
Effect
.
const runPromise: <string, never>(effect: Effect.Effect<string, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<string>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
const program2: Effect.Effect<string, never, never>
program2
).
(method) Promise<string>.then<void, never>(onfulfilled?: ((value: 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
)
22
// Output: "failure: Uh oh!"

We can choose to ignore the success and failure values if we’re not interested in them:

1
import {
import Effect
Effect
} from "effect"
2
import {
(alias) const constVoid: LazyArg<void> import constVoid

A thunk that returns always `void`.

constVoid
} from "effect/Function"
3
4
const
const task: Effect.Effect<number, string, never>
task
=
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>
fail
("Uh oh!").
(method) Pipeable.pipe<Effect.Effect<never, string, never>, Effect.Effect<number, string, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<never, string, never>) => Effect.Effect<number, string, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const as: <number>(value: number) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<number, E, R> (+1 overload)

This function maps the success value of an `Effect` value to a specified constant value.

as
(5))
5
6
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const match: <number, string, never, void, void>(self: Effect.Effect<number, string, never>, options: { readonly onFailure: (error: string) => void; readonly onSuccess: (value: number) => void; }) => Effect.Effect<...> (+1 overload)

Folds over the failure value or the success value to yield an effect that does not fail, but succeeds with the value returned by the left or right function passed to `match`.

match
(
const task: Effect.Effect<number, string, never>
task
, {
7
(property) onFailure: (error: string) => void
onFailure
:
(alias) const constVoid: LazyArg<void> import constVoid

A thunk that returns always `void`.

constVoid
,
8
(property) onSuccess: (value: number) => void
onSuccess
:
(alias) const constVoid: LazyArg<void> import constVoid

A thunk that returns always `void`.

constVoid
9
})

In this case, we use the constVoid function from the Function module, which constantly returns void, to provide handlers that perform no operation. This effectively discards the success and failure values and focuses solely on the control flow or side effects of the program.

Alternatively, we can achieve the same result using the Effect.ignore function:

1
import {
import Effect
Effect
} from "effect"
2
3
const
const task: Effect.Effect<number, string, never>
task
=
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>
fail
("Uh oh!").
(method) Pipeable.pipe<Effect.Effect<never, string, never>, Effect.Effect<number, string, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<never, string, never>) => Effect.Effect<number, string, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const as: <number>(value: number) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<number, E, R> (+1 overload)

This function maps the success value of an `Effect` value to a specified constant value.

as
(5))
4
5
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const ignore: <number, string, never>(self: Effect.Effect<number, string, never>) => Effect.Effect<void, never, never>

Returns a new effect that ignores the success or failure of this effect.

ignore
(
const task: Effect.Effect<number, string, never>
task
)

In addition to Effect.match, we have the Effect.matchEffect function, which allows us to handle success and failure cases while performing additional side effects.

Example

1
import {
import Effect
Effect
} from "effect"
2
3
const
const success: Effect.Effect<number, Error, never>
success
:
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
<number,
interface Error
Error
> =
import Effect
Effect
.
const succeed: <number>(value: number) => Effect.Effect<number, never, never>
succeed
(42)
4
const
const failure: Effect.Effect<number, Error, never>
failure
:
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
<number,
interface Error
Error
> =
import Effect
Effect
.
const fail: <Error>(error: Error) => Effect.Effect<never, Error, never>
fail
(
5
new
var Error: ErrorConstructor new (message?: string) => Error
Error
("Uh oh!")
6
)
7
8
const
const program1: Effect.Effect<string, never, never>
program1
=
import Effect
Effect
.
const matchEffect: <number, Error, never, string, never, never, string, never, never>(self: Effect.Effect<number, Error, never>, options: { readonly onFailure: (e: Error) => Effect.Effect<string, never, never>; readonly onSuccess: (a: number) => Effect.Effect<...>; }) => Effect.Effect<...> (+1 overload)
matchEffect
(
const success: Effect.Effect<number, Error, never>
success
, {
9
(property) onFailure: (e: Error) => Effect.Effect<string, never, never>
onFailure
: (
(parameter) error: Error
error
) =>
10
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
(`failure: ${
(parameter) error: Error
error
.
(property) Error.message: string
message
}`).
(method) Pipeable.pipe<Effect.Effect<string, never, never>, Effect.Effect<string, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<string, never, never>) => Effect.Effect<string, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
11
import Effect
Effect
.
const tap: <string, Effect.Effect<void, never, never>>(f: (a: string) => Effect.Effect<void, never, never>) => <E, R>(self: Effect.Effect<string, E, R>) => Effect.Effect<...> (+7 overloads)
tap
(
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
)
12
),
13
(property) onSuccess: (a: number) => Effect.Effect<string, never, never>
onSuccess
: (
(parameter) value: number
value
) =>
14
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
(`success: ${
(parameter) value: number
value
}`).
(method) Pipeable.pipe<Effect.Effect<string, never, never>, Effect.Effect<string, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<string, never, never>) => Effect.Effect<string, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const tap: <string, Effect.Effect<void, never, never>>(f: (a: string) => Effect.Effect<void, never, never>) => <E, R>(self: Effect.Effect<string, E, R>) => Effect.Effect<...> (+7 overloads)
tap
(
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
))
15
})
16
17
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
(
import Effect
Effect
.
const runSync: <string, never>(effect: Effect.Effect<string, never, never>) => string
runSync
(
const program1: Effect.Effect<string, never, never>
program1
))
18
/*
19
Output:
20
timestamp=... level=INFO fiber=#0 message="success: 42"
21
success: 42
22
*/
23
24
const
const program2: Effect.Effect<string, never, never>
program2
=
import Effect
Effect
.
const matchEffect: <number, Error, never, string, never, never, string, never, never>(self: Effect.Effect<number, Error, never>, options: { readonly onFailure: (e: Error) => Effect.Effect<string, never, never>; readonly onSuccess: (a: number) => Effect.Effect<...>; }) => Effect.Effect<...> (+1 overload)
matchEffect
(
const failure: Effect.Effect<number, Error, never>
failure
, {
25
(property) onFailure: (e: Error) => Effect.Effect<string, never, never>
onFailure
: (
(parameter) error: Error
error
) =>
26
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
(`failure: ${
(parameter) error: Error
error
.
(property) Error.message: string
message
}`).
(method) Pipeable.pipe<Effect.Effect<string, never, never>, Effect.Effect<string, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<string, never, never>) => Effect.Effect<string, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
27
import Effect
Effect
.
const tap: <string, Effect.Effect<void, never, never>>(f: (a: string) => Effect.Effect<void, never, never>) => <E, R>(self: Effect.Effect<string, E, R>) => Effect.Effect<...> (+7 overloads)
tap
(
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
)
28
),
29
(property) onSuccess: (a: number) => Effect.Effect<string, never, never>
onSuccess
: (
(parameter) value: number
value
) =>
30
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
(`success: ${
(parameter) value: number
value
}`).
(method) Pipeable.pipe<Effect.Effect<string, never, never>, Effect.Effect<string, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<string, never, never>) => Effect.Effect<string, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const tap: <string, Effect.Effect<void, never, never>>(f: (a: string) => Effect.Effect<void, never, never>) => <E, R>(self: Effect.Effect<string, E, R>) => Effect.Effect<...> (+7 overloads)
tap
(
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
))
31
})
32
33
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
(
import Effect
Effect
.
const runSync: <string, never>(effect: Effect.Effect<string, never, never>) => string
runSync
(
const program2: Effect.Effect<string, never, never>
program2
))
34
/*
35
Output:
36
timestamp=... level=INFO fiber=#1 message="failure: Uh oh!"
37
failure: Uh oh!
38
*/

Effect also provides Effect.matchCause and Effect.matchCauseEffect functions, which are useful for accessing the full cause of the underlying fiber in case of failure. This allows us to handle different failure causes separately and take appropriate actions.

Example (matchCauseEffect)

1
import {
import Effect
Effect
,
import Console
Console
} from "effect"
2
3
const
const task: Effect.Effect<number, Error, never>
task
:
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
<number,
interface Error
Error
> =
import Effect
Effect
.
const die: (defect: unknown) => Effect.Effect<never>
die
("Uh oh!")
4
5
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const matchCauseEffect: <number, Error, never, void, never, never, void, never, never>(self: Effect.Effect<number, Error, never>, options: { readonly onFailure: (cause: Cause<Error>) => Effect.Effect<void, never, never>; readonly onSuccess: (a: number) => Effect.Effect<...>; }) => Effect.Effect<...> (+1 overload)
matchCauseEffect
(
const task: Effect.Effect<number, Error, never>
task
, {
6
(property) onFailure: (cause: Cause<Error>) => Effect.Effect<void, never, never>
onFailure
: (
(parameter) cause: Cause<Error>
cause
) => {
7
switch (
(parameter) cause: Cause<Error>
cause
.
(property) _tag: "Empty" | "Die" | "Interrupt" | "Fail" | "Sequential" | "Parallel"
_tag
) {
8
case "Fail":
9
return
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
(`Fail: ${
(parameter) cause: Fail<Error>
cause
.
(property) Fail<Error>.error: Error
error
.
(property) Error.message: string
message
}`)
10
case "Die":
11
return
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
(`Die: ${
(parameter) cause: Die
cause
.
(property) Die.defect: unknown
defect
}`)
12
case "Interrupt":
13
return
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
(`${
(parameter) cause: Interrupt
cause
.
(property) Interrupt.fiberId: FiberId
fiberId
} interrupted!`)
14
}
15
return
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
("failed due to other causes")
16
},
17
(property) onSuccess: (a: number) => Effect.Effect<void, never, never>
onSuccess
: (
(parameter) value: number
value
) =>
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
(`succeeded with ${
(parameter) value: number
value
} value`)
18
})
19
20
import Effect
Effect
.
const runSync: <void, never>(effect: Effect.Effect<void, never, never>) => void
runSync
(
const program: Effect.Effect<void, never, never>
program
)
21
// Output: "Die: Uh oh!"