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 {
  return then((resultT) async {
    if (resultT.isOk) {
      return resultT;
    }
    try {
      var res = await f();
      if (res == null) {
        return Err<T>(StateError('null returned from $f'));
      }
      return Ok(res);
    } catch (e) {
      return Err<T>(StateError('Raised exception $e while called $f'));
    }
  });
}