• Runs an async 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: (() => Promise<T>)

      The async function to be run.

        • (): Promise<T>
        • Returns Promise<T>

    Returns Promise<Result<T, E>>

    A Promise that resolves to a Result that is the result of running the given async function.

    const result = await Result.runAsync(async () => {
    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'
    }