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)

import {
import Exit
Exit
} from "effect"
// Create an Exit representing a successful outcome with the value 42
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.

@since2.0.0

succeed
(42)
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const successExit: Exit.Exit<number, never>
successExit
)
// 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)

import {
import Exit
Exit
,
import Cause
Cause
} from "effect"
// Create an Exit representing a failure with an error message
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.

@since2.0.0

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

Constructs a new Fail cause from the specified error.

@since2.0.0

fail
("Something went wrong"))
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const failureExit: Exit.Exit<never, Cause.Cause<string>>
failureExit
)
/*
Output:
{
_id: 'Exit',
_tag: 'Failure',
cause: { _id: 'Cause', _tag: 'Fail', failure: 'Something went wrong' }
}
*/

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)

import {
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
,
import Exit
Exit
,
import Cause
Cause
} from "effect"
const
const simulatedSuccess: Exit.Exit<number, never>
simulatedSuccess
=
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

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.

@example

import { Effect } from "effect"

// Execute a successful effect and get the Exit result const result1 = Effect.runSyncExit(Effect.succeed(1)) console.log(result1) // Output: // { // _id: "Exit", // _tag: "Success", // value: 1 // }

// Execute a failing effect and get the Exit result const result2 = Effect.runSyncExit(Effect.fail("my error")) console.log(result2) // Output: // { // _id: "Exit", // _tag: "Failure", // cause: { // _id: "Cause", // _tag: "Fail", // failure: "my error" // } // }

@since2.0.0

runSyncExit
(
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

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.

@example

import { Effect } from "effect"

// Creating an effect that succeeds with the number 42 const success = Effect.succeed(42)

@since2.0.0

succeed
(1))
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
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)

@since2.0.0

match
(
const simulatedSuccess: Exit.Exit<number, never>
simulatedSuccess
, {
onFailure: (cause: Cause.Cause<never>) => string
onFailure
: (
cause: Cause.Cause<never>
cause
) =>
`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.

@since2.0.0

pretty
(
cause: Cause.Cause<never>
cause
)}`,
onSuccess: (a: number) => string
onSuccess
: (
value: number
value
) => `Exited with success value: ${
value: number
value
}`
})
)
// Output: "Exited with success value: 1"
const
const simulatedFailure: Exit.Exit<never, string>
simulatedFailure
=
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

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.

@example

import { Effect } from "effect"

// Execute a successful effect and get the Exit result const result1 = Effect.runSyncExit(Effect.succeed(1)) console.log(result1) // Output: // { // _id: "Exit", // _tag: "Success", // value: 1 // }

// Execute a failing effect and get the Exit result const result2 = Effect.runSyncExit(Effect.fail("my error")) console.log(result2) // Output: // { // _id: "Exit", // _tag: "Failure", // cause: { // _id: "Cause", // _tag: "Fail", // failure: "my error" // } // }

@since2.0.0

runSyncExit
(
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

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

catchAll

or

catchTag

.

@example

import { Effect } from "effect"

// Example of creating a failed effect const failedEffect = Effect.fail("Something went wrong")

// Handle the failure failedEffect.pipe( Effect.catchAll((error) => Effect.succeed(Recovered from: ${error})), Effect.runPromise ).then(console.log) // Output: "Recovered from: Something went wrong"

@since2.0.0

fail
("error"))
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
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)

@since2.0.0

match
(
const simulatedFailure: Exit.Exit<never, string>
simulatedFailure
, {
onFailure: (cause: Cause.Cause<string>) => string
onFailure
: (
cause: Cause.Cause<string>
cause
) =>
`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.

@since2.0.0

pretty
(
cause: Cause.Cause<string>
cause
)}`,
onSuccess: (a: never) => string
onSuccess
: (
value: never
value
) => `Exited with success value: ${
value: never
value
}`
})
)
// 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.