result
if it is an Ok
, otherwise defaultResult
.
Provide a fallback for a given Result
. Behaves like a logical
or
: if the result
value is an Ok
, returns that result
;
otherwise, returns the defaultResult
value.
This is useful when you want to make sure that something which takes a
Result
always ends up getting an Ok
variant, by supplying a default value
for the case that you currently have an Err
.
import { ok, err, Result, or } from 'true-utils/result';
const okA = ok<string, string>('a');
const okB = ok<string, string>('b');
const anErr = err<string, string>(':wat:');
const anotherErr = err<string, string>(':headdesk:');
console.log(or(okB, okA).toString()); // Ok(A)
console.log(or(anErr, okA).toString()); // Ok(A)
console.log(or(okB, anErr).toString()); // Ok(B)
console.log(or(anotherErr, anErr).toString()); // Err(:headdesk:)
result
if it is an Ok
, otherwise defaultResult
.
Provide a fallback for a given
Result
. Behaves like a logicalor
: if theresult
value is anOk
, returns thatresult
; otherwise, returns thedefaultResult
value.This is useful when you want to make sure that something which takes a
Result
always ends up getting anOk
variant, by supplying a default value for the case that you currently have anErr
.