ReaderTaskEither<E, L, R>.tryCatch constructor
ReaderTaskEither<E, L, R>.tryCatch (
- Future<
R> run(- E
- L onError(
- Object error,
- 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 ononError
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));
}
});