tryCatch<A> function

TaskOption<A> tryCatch<A>(
  1. Lazy<FutureOr<A>> task
)

Runs the given task, and wraps the result with Some. If it throws and error, it will return None.

expect(
  await tryCatch(() async => 'hello')(),
  O.some('hello'),
);
expect(
  await tryCatch(() async => throw 'fail')(),
  O.none(),
);

Implementation

TaskOption<A> tryCatch<A>(Lazy<FutureOr<A>> task) =>
    TaskOption(Task(() => fromThrowable(
          task,
          onSuccess: O.some,
          onError: (error, stackTrace) => kNone,
        )));