Map over a Result
instance as in map
and get out the
value if result
is Ok
, or apply a function (orElseFn
) to the
value wrapped in the Err
to get a default value.
Like mapOr
but using a function to transform the error into a
usable value instead of simply using a default value.
import { ok, err, mapOrElse } from 'true-myth/result';
const summarize = (s: string) => `The response was: '${s}'`;
const getReason = (err: { code: number, reason: string }) => err.reason;
const okResponse = ok("Things are grand here.");
const mappedOkAndUnwrapped = mapOrElse(getReason, summarize, okResponse);
console.log(mappedOkAndUnwrapped); // The response was: 'Things are grand here.'
const errResponse = err({ code: 500, reason: 'Nothing at this endpoint!' });
const mappedErrAndUnwrapped = mapOrElse(getReason, summarize, errResponse);
console.log(mappedErrAndUnwrapped); // Nothing at this endpoint!
Map over a Result
instance as in map
and get out the
value if result
is Ok
, or apply a function (orElseFn
) to the
value wrapped in the Err
to get a default value.
Like mapOr
but using a function to transform the error into a
usable value instead of simply using a default value.
import { ok, err, mapOrElse } from 'true-myth/result';
const summarize = (s: string) => `The response was: '${s}'`;
const getReason = (err: { code: number, reason: string }) => err.reason;
const okResponse = ok("Things are grand here.");
const mappedOkAndUnwrapped = mapOrElse(getReason, summarize, okResponse);
console.log(mappedOkAndUnwrapped); // The response was: 'Things are grand here.'
const errResponse = err({ code: 500, reason: 'Nothing at this endpoint!' });
const mappedErrAndUnwrapped = mapOrElse(getReason, summarize, errResponse);
console.log(mappedErrAndUnwrapped); // Nothing at this endpoint!
Map over a
Result
instance as inmap
and get out the value ifresult
isOk
, or apply a function (orElseFn
) to the value wrapped in theErr
to get a default value.Like
mapOr
but using a function to transform the error into a usable value instead of simply using a default value.Examples