UNPKG

rust-result-js

Version:

Implementation of Rust's Result object used for error handling

14 lines (11 loc) 428 B
import { Err, Ok, Result } from '../results'; export type ResultFunction<T, K> = ( ok: (val: T) => unknown, err: (errVal: K) => unknown ) => unknown; export const createResult = <T, K>(fn: ResultFunction<T, K>) => new Promise<Result<T, K>>((res) => { const WrappedOk = (value: T) => res(Ok(value)); const WrappedErr = (errValue: K) => res(Err(errValue)); fn(WrappedOk, WrappedErr); });