UNPKG

option-t

Version:

A toolkit of Nullable/Option/Result type implementation in ECMAScript. Their APIs are inspired by Rust's `Option<T>` and `Result<T, E>`.

18 lines (17 loc) 804 B
import { unsafeUnwrapValueInErrWithoutAnyCheck, unsafeUnwrapValueInOkWithoutAnyCheck, } from './internal/intrinsics_unsafe.js'; import { isOk } from './result.js'; /** * Maps a `Result<T, E>` to `U` by applying _transformer_ to a contained `Ok(T)` value in _input_, * or a _recoverer_ function to a contained `Err(E)` value in _input_. * This function can be used to unpack a successful result while handling an error. */ export async function mapOrElseAsyncForResult(input, recoverer, transformer) { if (isOk(input)) { const inner = unsafeUnwrapValueInOkWithoutAnyCheck(input); const result = await transformer(inner); return result; } const err = unsafeUnwrapValueInErrWithoutAnyCheck(input); const fallback = await recoverer(err); return fallback; }