• Returns fn(v) if resA is an Ok(v), otherwise returns the error resA. This is an implementation of FlatMap. The order of arguments is reversed because of that it is useful for partial applying.

    Type Parameters

    • T

    • U

    • E

    Parameters

    • fn: ((t) => Result<E, U>)

      The function provides a second result.

    Returns ((resA) => Result<E, U>)

    fn() if resA is an Ok.

    Examples

    import { andThen, err, ok, Result } from "./result.ts";
    import { assertEquals } from "../deps.ts";

    const sqrtThenToString = andThen(
    (num: number): Result<string, string> =>
    num < 0
    ? err("num must not be negative")
    : ok(Math.sqrt(num).toString()),
    );

    assertEquals(sqrtThenToString(ok(4)), ok("2"));
    assertEquals(sqrtThenToString(ok(-1)), err("num must not be negative"));
    assertEquals(sqrtThenToString(err("not a number")), err("not a number"));

Generated using TypeDoc