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>`.
19 lines (18 loc) • 692 B
JavaScript
import { createOk, isErr } from '../core/result.js';
import { unsafeUnwrapValueInOkWithoutAnyCheck } from '../internal/intrinsics_unsafe.js';
/**
* Maps a `Result<T, E>` to `Result<U, E>` by applying a _transformer_ function
* to an contained `Ok(T)` value, leaving an `Err(E)` value untouched.
*
* This function can be used to compose the results of two functions.
*/
export async function mapAsyncForResult(input, transformer) {
if (isErr(input)) {
const fallback = input;
return fallback;
}
const inner = unsafeUnwrapValueInOkWithoutAnyCheck(input);
const mapped = await transformer(inner);
const result = createOk(mapped);
return result;
}