orAsync method

Future<Result<T>> orAsync(
  1. Future<T> f()
)

If this result is Ok, then returns it as is. If this result is Err, then call result = await f(). If result of call f() is null, then returns Err. If result of call f() is value, then return Ok(value). If call of f() throws exception, then returns Err.

Implementation

Future<Result<T>> orAsync(Future<T> Function() f) async {
  if (isOk) {
    return this;
  }
  try {
    var res = await f();
    if (res == null) {
      return Err<T>(StateError('Result of f() == null, where `f` is $f'));
    }
    return Ok(res);
  } catch (e) {
    return Err(StateError('Raised exception $e while called $f'));
  }
}