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.
succeed
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
Constructs a new Exit.Success containing the specified value of type A.
@since ― 2.0.0
succeed(42)
5
6
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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
This function constructs a Cause carrying an error of type E. It's used
when you want to represent a known or anticipated failure in your effectful
computations.
@see ― isFailure Check if a Cause contains a failure
@since ― 2.0.0
fail("Something went wrong"))
5
6
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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.
Runs an effect synchronously and returns the result as an Exit type.
Details
This function executes the provided effect synchronously and returns an Exit
type that encapsulates the outcome of the effect:
If the effect succeeds, the result is wrapped in a Success.
If the effect fails, it returns a Failure containing a Cause that explains
the failure.
If the effect involves asynchronous operations, this function will return a Failure
with a Die cause, indicating that it cannot resolve the effect synchronously.
This makes the function suitable for use only with effects that are synchronous
in nature.
When to Use
Use this function when:
You want to handle both success and failure outcomes in a structured way using the Exit type.
You are working with effects that are purely synchronous and do not involve asynchronous operations.
You need to debug or inspect failures, including their causes, in a detailed manner.
Avoid using this function for effects that involve asynchronous operations, as it will fail with a Die cause.
Creates an Effect that always succeeds with a given value.
When to Use
Use this function when you need an effect that completes successfully with a
specific value without any errors or external dependencies.
@see ― fail to create an effect that represents a failure.
@example
// Title: Creating a Successful Effect
import { Effect } from"effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
constsuccess= Effect.succeed(42)
@since ― 2.0.0
succeed(1))
4
5
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
This function pretty-prints the entire Cause, including any failures,
defects, and interruptions. It can be especially helpful for logging,
debugging, or displaying structured errors to users.
You can optionally pass options to configure how the error cause is
rendered. By default, it includes essential details of all errors in the
Cause.
@see ― prettyErrors Get a list of PrettyError objects instead of a single string.
Runs an effect synchronously and returns the result as an Exit type.
Details
This function executes the provided effect synchronously and returns an Exit
type that encapsulates the outcome of the effect:
If the effect succeeds, the result is wrapped in a Success.
If the effect fails, it returns a Failure containing a Cause that explains
the failure.
If the effect involves asynchronous operations, this function will return a Failure
with a Die cause, indicating that it cannot resolve the effect synchronously.
This makes the function suitable for use only with effects that are synchronous
in nature.
When to Use
Use this function when:
You want to handle both success and failure outcomes in a structured way using the Exit type.
You are working with effects that are purely synchronous and do not involve asynchronous operations.
You need to debug or inspect failures, including their causes, in a detailed manner.
Avoid using this function for effects that involve asynchronous operations, as it will fail with a Die cause.
Creates an Effect that represents a recoverable error.
When to Use
Use this function to explicitly signal an error in an Effect. The error
will keep propagating unless it is handled. You can handle the error with
functions like
catchAll
or
catchTag
.
@see ― succeed to create an effect that represents a successful value.
@example
// Title: Creating a Failed Effect
import { Effect } from"effect"
// ┌─── Effect<never, Error, never>
// ▼
constfailure= Effect.fail(
newError("Operation failed due to network error")
)
@since ― 2.0.0
fail("error"))
15
16
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
This function pretty-prints the entire Cause, including any failures,
defects, and interruptions. It can be especially helpful for logging,
debugging, or displaying structured errors to users.
You can optionally pass options to configure how the error cause is
rendered. By default, it includes essential details of all errors in the
Cause.
@see ― prettyErrors Get a list of PrettyError objects instead of a single string.
@since ― 2.0.0
pretty(
cause: Cause.Cause<string>
cause)}`,
20
onSuccess: (a:never) => string
onSuccess: (
value: never
value) =>`Exited with success value: ${
value: never
value}`
21
})
22
)
23
// Output: "Exited with failure state: Error: error"
Exit vs Either
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 vs Effect
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.