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>`.
17 lines (16 loc) • 623 B
JavaScript
import { unsafeUnwrapValueInOkWithoutAnyCheck } from './internal/intrinsics_unsafe.js';
import { isErr } from './result.js';
/**
* Return the result of _transformer_ with using _input_ as an argument for it if _input_ is `Ok(T)`.
* Otherwise, return _defaultValue_.
*
* Basically, this operation is a combination `mapAsync()` and `unwrapOr()`.
*/
export async function mapOrAsyncForResult(input, defaultValue, transformer) {
if (isErr(input)) {
return defaultValue;
}
const source = unsafeUnwrapValueInOkWithoutAnyCheck(input);
const result = await transformer(source);
return result;
}