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 typeA
.Exit.Failure
: Contains a failure Cause of typeE
.
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)
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)
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)
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 asExit.succeed
.Effect.fail
is the same asExit.fail
.