or method

Future<Result<T>> or(
  1. T? f()
)

If this result is Ok, then returns it as is. If this result is Err, then call result = 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>> or(T? Function() f) {
  return then((resultT) {
    if (resultT.isOk) {
      return resultT;
    }
    try {
      var res = 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'));
    }
  });
}