True Myth / result / unwrapOrElse
Function: unwrapOrElse()
Call Signature
unwrapOrElse<
T,U,E>(orElseFn,result):T|U
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)); // 13Type Parameters
T
T
The value wrapped in the Ok.
U
U
E
E
The value wrapped in the Err.
Parameters
orElseFn
(error) => U
A function applied to the value wrapped in result if it is an Err, to generate the final value.
result
Result<T, E>
The result to unwrap if it is an Ok.
Returns
T | U
The value wrapped in result if it is Ok or the value returned by orElseFn applied to the value in Err.
Call Signature
unwrapOrElse<
T,U,E>(orElseFn): (result) =>T|U
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)); // 13Type Parameters
T
T
The value wrapped in the Ok.
U
U
E
E
The value wrapped in the Err.
Parameters
orElseFn
(error) => U
A function applied to the value wrapped in result if it is an Err, to generate the final value.
Returns
The value wrapped in result if it is Ok or the value returned by orElseFn applied to the value in Err.
(
result):T|U
Parameters
result
Result<T, E>
Returns
T | U