of<O, E> static method

Future<Result<O, E>> of<O, E>(
  1. FutureOr<O> function()
)

Creates a Result from an asynchronous callback Can be used to simplify the creation of a result when the error type is known.

Catches only the error defined by the type. The type of Err cannot be null. Setting Err type to dynamic catches all errors.

Implementation

static Future<Result<O, E>> of<O, E>(FutureOr<O> Function() function) async {
  assert(E != Null);
  try {
    final result = await function();
    return Ok(result);
    // ignore: nullable_type_in_catch_clause
  } on E catch (e) {
    return Err(e);
  }
}