Function unwrapOrElse

  • Safely get the value out of a Maybe by returning the wrapped value if it is Just, or by applying orElseFn if it is Nothing.

    This is useful when you need to generate a value (e.g. by using current values in the environment – whether preloaded or by local closure) instead of having a single default value available (as in unwrapOr).

    import Maybe from 'true-myth/maybe';

    // You can imagine that someOtherValue might be dynamic.
    const someOtherValue = 99;
    const handleNothing = () => someOtherValue;

    const aJust = Maybe.just(42);
    console.log(Maybe.unwrapOrElse(handleNothing, aJust)); // 42

    const aNothing = nothing<number>();
    console.log(Maybe.unwrapOrElse(handleNothing, aNothing)); // 99

    Type Parameters

    • T

      The wrapped value.

    • U

    Parameters

    • orElseFn: () => U

      A function used to generate a valid value if maybe is a Nothing.

    • maybe: Maybe<T>

      The Maybe instance to unwrap if it is a Just

    Returns T | U

    Either the content of maybe or the value returned from orElseFn.

  • Safely get the value out of a Maybe by returning the wrapped value if it is Just, or by applying orElseFn if it is Nothing.

    This is useful when you need to generate a value (e.g. by using current values in the environment – whether preloaded or by local closure) instead of having a single default value available (as in unwrapOr).

    import Maybe from 'true-myth/maybe';

    // You can imagine that someOtherValue might be dynamic.
    const someOtherValue = 99;
    const handleNothing = () => someOtherValue;

    const aJust = Maybe.just(42);
    console.log(Maybe.unwrapOrElse(handleNothing, aJust)); // 42

    const aNothing = nothing<number>();
    console.log(Maybe.unwrapOrElse(handleNothing, aNothing)); // 99

    Type Parameters

    • T

      The wrapped value.

    • U

    Parameters

    • orElseFn: () => U

      A function used to generate a valid value if maybe is a Nothing.

    Returns (maybe: Maybe<T>) => T | U

    Either the content of maybe or the value returned from orElseFn.