ReaderTaskEither<E, L, R>.tryCatch constructor

ReaderTaskEither<E, L, R>.tryCatch(
  1. Future<R> run(
    1. E
    ),
  2. L onError(
    1. Object error,
    2. StackTrace stackTrace
    )
)

Execute an async function (Future) and convert the result to Either:

  • If the execution is successful, returns a Right
  • If the execution fails (throw), then return a Left based on onError

Used to work with Future and exceptions using Either instead of try/catch.

Implementation

factory ReaderTaskEither.tryCatch(
  Future<R> Function(E) run,
  L Function(Object error, StackTrace stackTrace) onError,
) =>
    ReaderTaskEither<E, L, R>((env) async {
      try {
        return Right<L, R>(await run(env));
      } catch (error, stack) {
        return Left<L, R>(onError(error, stack));
      }
    });