Function orElse

  • Like or, but using a function to construct the alternative Result.

    Sometimes you need to perform an operation using other data in the environment to construct the fallback value. In these situations, you can pass a function (which may be a closure) as the elseFn to generate the fallback Result<T>. It can then transform the data in the Err to something usable as an Ok, or generate a new Err instance as appropriate.

    Useful for transforming failures to usable data.

    Type Parameters

    • T
    • E
    • F

    Parameters

    • elseFn: (err: E) => Result<T, F>

      The function to apply to the contents of the Err if result is an Err, to create a new Result.

    • result: Result<T, E>

      The Result to use if it is an Ok.

    Returns Result<T, F>

    The result if it is Ok, or the Result returned by elseFn if result is an `Err.

  • Like or, but using a function to construct the alternative Result.

    Sometimes you need to perform an operation using other data in the environment to construct the fallback value. In these situations, you can pass a function (which may be a closure) as the elseFn to generate the fallback Result<T>. It can then transform the data in the Err to something usable as an Ok, or generate a new Err instance as appropriate.

    Useful for transforming failures to usable data.

    Type Parameters

    • T
    • E
    • F

    Parameters

    • elseFn: (err: E) => Result<T, F>

      The function to apply to the contents of the Err if result is an Err, to create a new Result.

    Returns (result: Result<T, E>) => Result<T, F>

    The result if it is Ok, or the Result returned by elseFn if result is an `Err.