Function tryOr

  • Execute the provided callback, wrapping the return value in Ok or Err(error) if there is an exception.

    const aSuccessfulOperation = () => 2 + 2;

    const anOkResult = Result.tryOr('Oh noes!!1', () => {
    aSuccessfulOperation()
    }); // => Ok(4)

    const thisOperationThrows = () => throw new Error('Bummer');

    const anErrResult = Result.tryOr('Oh noes!!1', () => {
    thisOperationThrows();
    }); // => Err('Oh noes!!1')

    Type Parameters

    • T
    • E

    Parameters

    • error: E

      The error value in case of an exception

    • callback: () => T

      The callback to try executing

    Returns Result<T, E>

  • Execute the provided callback, wrapping the return value in Ok or Err(error) if there is an exception.

    const aSuccessfulOperation = () => 2 + 2;

    const anOkResult = Result.tryOr('Oh noes!!1', () => {
    aSuccessfulOperation()
    }); // => Ok(4)

    const thisOperationThrows = () => throw new Error('Bummer');

    const anErrResult = Result.tryOr('Oh noes!!1', () => {
    thisOperationThrows();
    }); // => Err('Oh noes!!1')

    Type Parameters

    • T
    • E

    Parameters

    • error: E

      The error value in case of an exception

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