Package: effect
Module: Result
Provides a flexible variant of flatMap that accepts multiple input shapes.
When to use
Use to sequence a next step that may be a Result, a function, or a plain
value.
Details
The second argument can be:
(a: A) => Result<A2, E2> (same as flatMap)(a: A) => A2 (auto-wrapped in succeed)Result<A2, E2> value (ignores the success of self)A2 (auto-wrapped in succeed, ignores self)If self is a Failure, the second argument is never evaluated.
Example (Chaining Result values with different argument types)
import { pipe, Result } from "effect"
// With a function returning a Result
const a = pipe(
Result.succeed(1),
Result.andThen((n) => Result.succeed(n + 1))
)
// With a plain mapping function
const b = pipe(
Result.succeed(1),
Result.andThen((n) => n + 1)
)
// With a constant value
const c = pipe(Result.succeed(1), Result.andThen("done"))
console.log(a, b, c)
See
flatMap for the stricter variant (function returning Result only)map when you always return a plain valueSignature
declare const andThen: { <A, A2, E2>(f: (a: A) => Result<A2, E2>): <E>(self: Result<A, E>) => Result<A2, E | E2>; <A2, E2>(f: Result<A2, E2>): <A, E>(self: Result<A, E>) => Result<A2, E | E2>; <A, A2>(f: (a: A) => A2): <E>(self: Result<A, E>) => Result<A2, E>; <A2>(right: NotFunction<A2>): <A, E>(self: Result<A, E>) => Result<A2, E>; <A, E, A2, E2>(self: Result<A, E>, f: (a: A) => Result<A2, E2>): Result<A2, E | E2>; <A, E, A2, E2>(self: Result<A, E>, f: Result<A2, E2>): Result<A2, E | E2>; <A, E, A2>(self: Result<A, E>, f: (a: A) => A2): Result<A2, E>; <A, E, A2>(self: Result<A, E>, f: NotFunction<A2>): Result<A2, E>; }
Since v2.0.0