result.andThen

The result of a fallible next step on a success, with diagnostics accumulated; a failure short-circuits.

Value helperSince 0.3.0

Usage

TypeScript
Result.andThen<T, U>(r: Result<T>, f: (value: T) => Result<U>): Result<U>

Parameters

  • rResult<T>

    The result to read.

    Required

  • f(value: T) => Result<U>

    The next step, returning a Result.

    Required

Returns

A failure of r unchanged (f is not called). Otherwise f’s result with r’s diagnostics added: before the step’s on success, after them on failure, so a failure’s first diagnostic is still the step’s error.

Example

TypeScript
Result.andThen(Result.ok(4), (n) => (n > 0 ? Result.ok(Math.sqrt(n)) : Result.fail('GEOMETRY_INPUT_INVALID', 'negative'))).value // → 2

See also