Skip to content

True Myth / result / orElse

Function: orElse()

Call Signature

orElse<T, E, R>(elseFn, result): Result<T | OkFor<R>, ErrFor<R>>

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

Sometimes you need to perform an operation using the error value (and possibly 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

T

E

E

R

R extends AnyResult

Parameters

elseFn

(err) => R

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 | OkFor<R>, ErrFor<R>>

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

Call Signature

orElse<T, E, R>(elseFn): (result) => Result<T | OkFor<R>, ErrFor<R>>

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

Sometimes you need to perform an operation using the error value (and possibly 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

T

E

E

R

R extends AnyResult

Parameters

elseFn

(err) => R

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

Returns

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

(result): Result<T | OkFor<R>, ErrFor<R>>

Parameters

result

Result<T, E>

Returns

Result<T | OkFor<R>, ErrFor<R>>