Function tryOrElse

  • Execute the provided callback, wrapping the return value in Ok. If there is an exception, return a Err of whatever the onError function returns.

    const aSuccessfulOperation = () => 2 + 2;

    const anOkResult = Result.tryOrElse(
    (e) => e,
    aSuccessfulOperation
    ); // => Ok(4)

    const thisOperationThrows = () => throw 'Bummer'

    const anErrResult = Result.tryOrElse((e) => e, () => {
    thisOperationThrows();
    }); // => Err('Bummer')

    Type Parameters

    • T
    • E

    Parameters

    • onError: (e: unknown) => E

      A function that takes e exception and returns what will be wrapped in a Result.Err

    • callback: () => T

      The callback to try executing

    Returns Result<T, E>

  • Execute the provided callback, wrapping the return value in Ok. If there is an exception, return a Err of whatever the onError function returns.

    const aSuccessfulOperation = () => 2 + 2;

    const anOkResult = Result.tryOrElse(
    (e) => e,
    aSuccessfulOperation
    ); // => Ok(4)

    const thisOperationThrows = () => throw 'Bummer'

    const anErrResult = Result.tryOrElse((e) => e, () => {
    thisOperationThrows();
    }); // => Err('Bummer')

    Type Parameters

    • T
    • E

    Parameters

    • onError: (e: unknown) => E

      A function that takes e exception and returns what will be wrapped in a Result.Err

    Returns (callback: () => T) => Result<T, E>