catching<T, E extends Object> static method

FutureOr<Result<T, E>> catching<T, E extends Object>(
  1. FutureOr<T> closure()
)

Creates a success result from the return value of an async closure. Without an explicit type parameters, any object thrown by closure is caught and returned in a failure result. Specifying the type parameters will catch only objects of type E. All others continue uncaught. To specify E Dart requires that you also specify T.

Implementation

static FutureOr<Result<T, E>> catching<T, E extends Object>(
    FutureOr<T> Function() closure) async {
  try {
    final value = await closure();
    return Result.success(value);
  } on E catch (e) {
    return Result.failure(e);
  }
}