<A, E, R>(self: Effect<A, E, R>): Effect<Result.Result<A, E>, never, R>Converts both success and failure of an Effect into a Result type.
When to use
Use when you want an Effect's typed failures to be handled as Result
data while preserving the original error value.
Details
This function converts an effect that may fail into an effect that always
succeeds, wrapping the outcome in a Result type. The result will be
Result.Failure if the effect fails, containing the recoverable error, or
Result.Success if it succeeds, containing the result.
Using this function, you can handle recoverable errors explicitly without causing the effect to fail. This is particularly useful in scenarios where you want to chain effects and manage both success and failure in the same logical flow.
The resulting effect cannot fail directly because all recoverable failures
are represented inside the Result type.
Gotchas
result only captures typed, recoverable failures. Defects and
interruptions are not captured inside the Result and still fail the
effect.
Example (Capturing success or failure as Result)
import { Effect } from "effect"
const success = Effect.succeed(42)
const failure = Effect.fail("Something went wrong")
const program1 = Effect.result(success)
const program2 = Effect.result(failure)
Effect.runPromise(program1).then(console.log)
// { _id: 'Result', _tag: 'Success', value: 42 }
Effect.runPromise(program2).then(console.log)
// { _id: 'Result', _tag: 'Failure', error: 'Something went wrong' }export const const result: <A, E, R>(
self: Effect<A, E, R>
) => Effect<Result.Result<A, E>, never, R>
Converts both success and failure of an Effect into a Result type.
When to use
Use when you want an Effect's typed failures to be handled as Result
data while preserving the original error value.
Details
This function converts an effect that may fail into an effect that always
succeeds, wrapping the outcome in a Result type. The result will be
Result.Failure if the effect fails, containing the recoverable error, or
Result.Success if it succeeds, containing the result.
Using this function, you can handle recoverable errors explicitly without
causing the effect to fail. This is particularly useful in scenarios where
you want to chain effects and manage both success and failure in the same
logical flow.
The resulting effect cannot fail directly because all recoverable failures
are represented inside the Result type.
Gotchas
result only captures typed, recoverable failures. Defects and
interruptions are not captured inside the Result and still fail the
effect.
Example (Capturing success or failure as Result)
import { Effect } from "effect"
const success = Effect.succeed(42)
const failure = Effect.fail("Something went wrong")
const program1 = Effect.result(success)
const program2 = Effect.result(failure)
Effect.runPromise(program1).then(console.log)
// { _id: 'Result', _tag: 'Success', value: 42 }
Effect.runPromise(program2).then(console.log)
// { _id: 'Result', _tag: 'Failure', error: 'Something went wrong' }
result: <function (type parameter) A in <A, E, R>(self: Effect<A, E, R>): Effect<Result.Result<A, E>, never, R>A, function (type parameter) E in <A, E, R>(self: Effect<A, E, R>): Effect<Result.Result<A, E>, never, R>E, function (type parameter) R in <A, E, R>(self: Effect<A, E, R>): Effect<Result.Result<A, E>, never, R>R>(self: Effect<A, E, R>(parameter) self: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: interface Effect<out A, out E = never, out R = never>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.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
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<function (type parameter) A in <A, E, R>(self: Effect<A, E, R>): Effect<Result.Result<A, E>, never, R>A, function (type parameter) E in <A, E, R>(self: Effect<A, E, R>): Effect<Result.Result<A, E>, never, R>E, function (type parameter) R in <A, E, R>(self: Effect<A, E, R>): Effect<Result.Result<A, E>, never, R>R>) => interface Effect<out A, out E = never, out R = never>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.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
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<import ResultResult.type Result<A, E = never> = Result.Success<A, E> | Result.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<function (type parameter) A in <A, E, R>(self: Effect<A, E, R>): Effect<Result.Result<A, E>, never, R>A, function (type parameter) E in <A, E, R>(self: Effect<A, E, R>): Effect<Result.Result<A, E>, never, R>E>, never, function (type parameter) R in <A, E, R>(self: Effect<A, E, R>): Effect<Result.Result<A, E>, never, R>R> = import internalinternal.const result: <A, E, R>(
self: Effect.Effect<A, E, R>
) => Effect.Effect<Result.Result<A, E>, never, R>
result