Skip to content

True Myth / result / tryOrElse

Function: tryOrElse()

Call Signature

tryOrElse<T, E>(onError, callback): 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.

ts
import { tryOrElse } from 'true-myth/result';

const aSuccessfulOperation = () => 2 + 2;

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

const thisOperationThrows = () => throw 'Bummer'

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

Type Parameters

T

T

E

E

Parameters

onError

(e) => 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>

Call Signature

tryOrElse<T, E>(onError): (callback) => 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.

ts
import { tryOrElse } from 'true-myth/result';

const aSuccessfulOperation = () => 2 + 2;

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

const thisOperationThrows = () => throw 'Bummer'

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

Type Parameters

T

T

E

E

Parameters

onError

(e) => E

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

Returns

(callback): Result<T, E>

Parameters

callback

() => T

Returns

Result<T, E>