Skip to content

Exit

An Exit<A, E> describes the result of running an Effect workflow.

There are two possible states for an Exit<A, E>:

  • Exit.Success: Contains a success value of type A.
  • Exit.Failure: Contains a failure Cause of type E.

The Exit module provides two primary functions for constructing exit values: Exit.succeed and Exit.fail. These functions represent the outcomes of an effectful computation in terms of success or failure.

Exit.succeed creates an Exit value that represents a successful outcome. You use this function when you want to indicate that a computation completed successfully and to provide the resulting value.

Example (Creating a Successful Exit)

1
import {
import Exit
Exit
} from "effect"
2
3
// Create an Exit representing a successful outcome with the value 42
4
const
const successExit: Exit.Exit<number, never>
successExit
=
import Exit
Exit
.
const succeed: <number>(value: number) => Exit.Exit<number, never>

Constructs a new `Exit.Success` containing the specified value of type `A`.

succeed
(42)
5
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) 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
(
const successExit: Exit.Exit<number, never>
successExit
)
7
// Output: { _id: 'Exit', _tag: 'Success', value: 42 }

Exit.fail creates an Exit value that represents a failure. The failure is described using a Cause object, which can encapsulate expected errors, defects, interruptions, or even composite errors.

Example (Creating a Failed Exit)

1
import {
import Exit
Exit
,
import Cause
Cause
} from "effect"
2
3
// Create an Exit representing a failure with an error message
4
const
const failureExit: Exit.Exit<never, Cause.Cause<string>>
failureExit
=
import Exit
Exit
.
const fail: <Cause.Cause<string>>(error: Cause.Cause<string>) => Exit.Exit<never, Cause.Cause<string>>

Constructs a new `Exit.Failure` from the specified recoverable error of type `E`.

fail
(
import Cause
Cause
.
const fail: <string>(error: string) => Cause.Cause<string>

Constructs a new `Fail` cause from the specified `error`.

fail
("Something went wrong"))
5
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) 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
(
const failureExit: Exit.Exit<never, Cause.Cause<string>>
failureExit
)
7
/*
8
Output:
9
{
10
_id: 'Exit',
11
_tag: 'Failure',
12
cause: { _id: 'Cause', _tag: 'Fail', failure: 'Something went wrong' }
13
}
14
*/

You can handle different outcomes of an Exit using the Exit.match function. This function lets you provide two separate callbacks to handle both success and failure cases of an Effect execution.

Example (Matching Success and Failure States)

1
import {
import Effect
Effect
,
import Exit
Exit
,
import Cause
Cause
} from "effect"
2
3
const
const simulatedSuccess: Exit.Exit<number, never>
simulatedSuccess
=
import Effect
Effect
.
const runSyncExit: <number, never>(effect: Effect.Effect<number, never, never>) => Exit.Exit<number, never>

Executes an effect synchronously and returns an `Exit` describing the result. Use `runSyncExit` when you need detailed information about the outcome of the effect, including whether it succeeded or failed, without throwing exceptions.

runSyncExit
(
import Effect
Effect
.
const succeed: <number>(value: number) => Effect.Effect<number, 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
(1))
4
5
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
(
6
import Exit
Exit
.
const match: <number, never, string, string>(self: Exit.Exit<number, never>, options: { readonly onFailure: (cause: Cause.Cause<never>) => string; readonly onSuccess: (a: number) => string; }) => string (+1 overload)
match
(
const simulatedSuccess: Exit.Exit<number, never>
simulatedSuccess
, {
7
(property) onFailure: (cause: Cause.Cause<never>) => string
onFailure
: (
(parameter) cause: Cause.Cause<never>
cause
) =>
8
`Exited with failure state: ${
import Cause
Cause
.
const pretty: <never>(cause: Cause.Cause<never>, options?: { readonly renderErrorCause?: boolean | undefined; }) => string

Returns the specified `Cause` as a pretty-printed string.

pretty
(
(parameter) cause: Cause.Cause<never>
cause
)}`,
9
(property) onSuccess: (a: number) => string
onSuccess
: (
(parameter) value: number
value
) => `Exited with success value: ${
(parameter) value: number
value
}`
10
})
11
)
12
// Output: "Exited with success value: 1"
13
14
const
const simulatedFailure: Exit.Exit<never, string>
simulatedFailure
=
import Effect
Effect
.
const runSyncExit: <never, string>(effect: Effect.Effect<never, string, never>) => Exit.Exit<never, string>

Executes an effect synchronously and returns an `Exit` describing the result. Use `runSyncExit` when you need detailed information about the outcome of the effect, including whether it succeeded or failed, without throwing exceptions.

runSyncExit
(
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
("error"))
15
16
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
(
17
import Exit
Exit
.
const match: <never, string, string, string>(self: Exit.Exit<never, string>, options: { readonly onFailure: (cause: Cause.Cause<string>) => string; readonly onSuccess: (a: never) => string; }) => string (+1 overload)
match
(
const simulatedFailure: Exit.Exit<never, string>
simulatedFailure
, {
18
(property) onFailure: (cause: Cause.Cause<string>) => string
onFailure
: (
(parameter) cause: Cause.Cause<string>
cause
) =>
19
`Exited with failure state: ${
import Cause
Cause
.
const pretty: <string>(cause: Cause.Cause<string>, options?: { readonly renderErrorCause?: boolean | undefined; }) => string

Returns the specified `Cause` as a pretty-printed string.

pretty
(
(parameter) cause: Cause.Cause<string>
cause
)}`,
20
(property) onSuccess: (a: never) => string
onSuccess
: (
(parameter) value: never
value
) => `Exited with success value: ${
(parameter) value: never
value
}`
21
})
22
)
23
// Output: "Exited with failure state: Error: error"

Conceptually, Exit<A, E> can be thought of as Either<A, Cause<E>>. However, the Cause type represents more than just expected errors of type E. It includes:

  • Interruption causes
  • Defects (unexpected errors)
  • The combination of multiple causes

This allows Cause to capture richer and more complex error states compared to a simple Either.

Exit is actually a subtype of Effect. This means that Exit values can also be considered as Effect values.

  • An Exit, in essence, is a “constant computation”.
  • Effect.succeed is essentially the same as Exit.succeed.
  • Effect.fail is the same as Exit.fail.