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) => E)

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

        • (e): E
        • Parameters

          • e: unknown

          Returns E

    • callback: (() => T)

      The callback to try executing

        • (): T
        • Returns T

    Returns Result<T, E>

  • Type Parameters

    • T

    • E

    Parameters

    • onError: ((e) => E)
        • (e): E
        • Parameters

          • e: unknown

          Returns E

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

      • (callback): Result<T, E>
      • Parameters

        • callback: (() => T)
            • (): T
            • Returns T

        Returns Result<T, E>

Generated using TypeDoc