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
A function used to generate a valid value if maybe
is a
Nothing
.
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 isJust
, or by applyingorElseFn
if it isNothing
.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
).