You can think of this like a short-circuiting logical "and" operation on a
Result
type. If result
is Ok
, then the result is the
andResult
. If result
is Err
, the result is the Err
.
This is useful when you have another Result
value you want to provide if and
only if you have an Ok
– that is, when you need to make sure that if you
Err
, whatever else you're handing a Result
to also gets that Err
.
Notice that, unlike in map
or its variants, the original result
is
not involved in constructing the new Result
.
import { and, ok, err, toString } from 'true-myth/result';
const okA = ok('A');
const okB = ok('B');
const anErr = err({ so: 'bad' });
console.log(toString(and(okB, okA))); // Ok(B)
console.log(toString(and(okB, anErr))); // Err([object Object])
console.log(toString(and(anErr, okA))); // Err([object Object])
console.log(toString(and(anErr, anErr))); // Err([object Object])
You can think of this like a short-circuiting logical "and" operation on a
Result
type. Ifresult
isOk
, then the result is theandResult
. Ifresult
isErr
, the result is theErr
.This is useful when you have another
Result
value you want to provide if and only if you have anOk
– that is, when you need to make sure that if youErr
, whatever else you're handing aResult
to also gets thatErr
.Notice that, unlike in
map
or its variants, the originalresult
is not involved in constructing the newResult
.Examples