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) • 682 B
JavaScript
import { createErr, isOk } from '../core/result.js';
import { unsafeUnwrapValueInErrWithoutAnyCheck } from '../internal/intrinsics_unsafe.js';
/**
* Maps a `Result<T, E>` to `Result<T, F>` by applying a _transformer_ function `mapFn<E, F>`
* to an contained `Err(E)` value, leaving an `Ok(T)` value untouched.
*
* This function can be used to pass through a successful result while handling an error.
*/
export async function mapErrAsyncForResult(input, transformer) {
if (isOk(input)) {
return input;
}
const err = unsafeUnwrapValueInErrWithoutAnyCheck(input);
const e = await transformer(err);
const result = createErr(e);
return result;
}