<A, E>(result: Result.Result<A, E>): Effect<A, E>Converts a Result to an Effect.
Example (Converting a Result into an Effect)
import { Effect, Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("Something went wrong")
const effect1 = Effect.fromResult(success)
const effect2 = Effect.fromResult(failure)
Effect.runPromise(effect1).then(console.log) // 42
Effect.runPromiseExit(effect2).then(console.log)
// { _id: 'Exit', _tag: 'Failure', cause: { _id: 'Cause', _tag: 'Fail', failure: 'Something went wrong' } }export const const fromResult: <A, E>(
result: Result.Result<A, E>
) => Effect<A, E>
Converts a Result to an Effect.
Example (Converting a Result into an Effect)
import { Effect, Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("Something went wrong")
const effect1 = Effect.fromResult(success)
const effect2 = Effect.fromResult(failure)
Effect.runPromise(effect1).then(console.log) // 42
Effect.runPromiseExit(effect2).then(console.log)
// { _id: 'Exit', _tag: 'Failure', cause: { _id: 'Cause', _tag: 'Fail', failure: 'Something went wrong' } }
fromResult: <function (type parameter) A in <A, E>(result: Result.Result<A, E>): Effect<A, E>A, function (type parameter) E in <A, E>(result: Result.Result<A, E>): Effect<A, E>E>(result: Result.Result<A, E>result: 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>(result: Result.Result<A, E>): Effect<A, E>A, function (type parameter) E in <A, E>(result: Result.Result<A, E>): Effect<A, E>E>) => 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>(result: Result.Result<A, E>): Effect<A, E>A, function (type parameter) E in <A, E>(result: Result.Result<A, E>): Effect<A, E>E> = import internalinternal.const fromResult: <A, E>(
result: Result.Result<A, E>
) => Effect.Effect<A, E>
fromResult