• Runs a function and returns a Result. If the function succeeds, the Ok is returned. If the function fails, the Err is returned.

    Type Parameters

    • T

      The type of the value.

    • E = Error

      The type of the error.

    Parameters

    • fn: (() => T)

      The function to be run.

        • (): T
        • Returns T

    Returns Result<T, E>

    A Result that is the result of running the given function.

    const result = Result.run(() => {
    if (Math.random() > 0.5) {
    return 'Success';
    } else {
    throw new Error('Failure');
    }
    });
    if (result.isOk()) {
    console.log(result.unwrap()); // 'Success'
    } else {
    console.error(result.unwrapError()); // Error: 'Failure'
    }