True Myth / result / mapOrElse
Function: mapOrElse()
Call Signature
mapOrElse<
T
,U
,E
>(orElseFn
,mapFn
,result
):U
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.
Examples
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!
Type Parameters
T
T
The type of the wrapped Ok
value.
U
U
The type of the resulting value from applying mapFn
to the Ok
value or orElseFn
to the Err
value.
E
E
The type of the wrapped Err
value.
Parameters
orElseFn
(err
) => U
The function to apply to the wrapped Err
value to get a usable value if result
is an Err
.
mapFn
(t
) => U
The function to apply to the wrapped Ok
value if result
is an Ok
.
result
Result
<T
, E
>
The Result
instance to map over.
Returns
U
Call Signature
mapOrElse<
T
,U
,E
>(orElseFn
,mapFn
): (result
) =>U
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.
Examples
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!
Type Parameters
T
T
The type of the wrapped Ok
value.
U
U
The type of the resulting value from applying mapFn
to the Ok
value or orElseFn
to the Err
value.
E
E
The type of the wrapped Err
value.
Parameters
orElseFn
(err
) => U
The function to apply to the wrapped Err
value to get a usable value if result
is an Err
.
mapFn
(t
) => U
The function to apply to the wrapped Ok
value if result
is an Ok
.
Returns
(
result
):U
Parameters
result
Result
<T
, E
>
Returns
U
Call Signature
mapOrElse<
T
,U
,E
>(orElseFn
): (mapFn
) => (result
) =>U
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.
Examples
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!
Type Parameters
T
T
The type of the wrapped Ok
value.
U
U
The type of the resulting value from applying mapFn
to the Ok
value or orElseFn
to the Err
value.
E
E
The type of the wrapped Err
value.
Parameters
orElseFn
(err
) => U
The function to apply to the wrapped Err
value to get a usable value if result
is an Err
.
Returns
(
mapFn
): (result
) =>U
Parameters
mapFn
(t
) => U
Returns
(
result
):U
Parameters
result
Result
<T
, E
>
Returns
U