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>`.
16 lines (15 loc) • 636 B
JavaScript
import { isOk } from '../core/result.js';
import { unsafeUnwrapValueInErrWithoutAnyCheck, unsafeUnwrapValueInOkWithoutAnyCheck, } from '../internal/intrinsics_unsafe.js';
/**
* Unwraps _input_, returns the content of an `Ok(T)`.
* If the value is an `Err(E)` then it calls `recoverer` with its value.
*/
export async function unwrapOrElseAsyncForResult(input, recoverer) {
if (isOk(input)) {
const value = unsafeUnwrapValueInOkWithoutAnyCheck(input);
return value;
}
const error = unsafeUnwrapValueInErrWithoutAnyCheck(input);
const defaultValue = await recoverer(error);
return defaultValue;
}