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) • 594 B
JavaScript
import { isNone, unwrapSome } from './option.js';
/**
* Maps a `Option<T>` to `U` by applying _transformer_ to a contained `Some(T)` value in _input_,
* or a _recoverer_ function to a contained `None` value in _input_.
* This function can be used to unpack a successful result while handling an error.
*/
export async function mapOrElseAsyncForOption(input, recoverer, transformer) {
if (isNone(input)) {
const fallback = await recoverer();
return fallback;
}
const inner = unwrapSome(input);
const result = await transformer(inner);
return result;
}