The value wrapped in result
if it is Ok
or the value
returned by orElseFn
applied to the value in Err
.
Safely get the value out of a Result
by returning the wrapped
value if it is Ok
, or by applying orElseFn
to the value in the
Err
.
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 { ok, err, unwrapOrElse } from 'true-myth/result';
// You can imagine that someOtherValue might be dynamic.
const someOtherValue = 2;
const handleErr = (errValue: string) => errValue.length + someOtherValue;
const anOk = ok<number, string>(42);
console.log(unwrapOrElse(handleErr, anOk)); // 42
const anErr = err<number, string>('oh teh noes');
console.log(unwrapOrElse(handleErr, anErr)); // 13
The value wrapped in result
if it is Ok
or the value
returned by orElseFn
applied to the value in Err
.
Safely get the value out of a
Result
by returning the wrapped value if it isOk
, or by applyingorElseFn
to the value in theErr
.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
).