Package: effect
Module: Option
Chains a second computation onto an Option. The second value can be a
plain value, an Option, or a function returning either.
When to use
Use when you need to chain an Option with a next step that may be another
Option, a plain value, or a function.
Details
self is None, returns None immediatelyf is a function, calls it with the Some valuef returns an Option, returns it as-is; if a plain value, wraps in Somef is not a function, uses it directly (same wrapping rules)Example (Chaining with andThen)
import { Option } from "effect"
// Chain with a function returning Option
console.log(Option.andThen(Option.some(5), (x) => Option.some(x * 2)))
// Output: { _id: 'Option', _tag: 'Some', value: 10 }
// Chain with a static value
console.log(Option.andThen(Option.some(5), "hello"))
// Output: { _id: 'Option', _tag: 'Some', value: "hello" }
// Chain with None - skips
console.log(Option.andThen(Option.none(), (x) => Option.some(x * 2)))
// Output: { _id: 'Option', _tag: 'None' }
See
flatMap for the standard monadic bindmap when you always return a plain valueSignature
declare const andThen: { <A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>; <B>(f: Option<B>): <A>(self: Option<A>) => Option<B>; <A, B>(f: (a: A) => B): (self: Option<A>) => Option<B>; <B>(f: NotFunction<B>): <A>(self: Option<A>) => Option<B>; <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>; <A, B>(self: Option<A>, f: Option<B>): Option<B>; <A, B>(self: Option<A>, f: (a: A) => B): Option<B>; <A, B>(self: Option<A>, f: NotFunction<B>): Option<B>; }
Since v2.0.0