pipeAsync<T2> method

Future<Result<T2>> pipeAsync<T2>(
  1. Future f(
    1. T
    )
)

If result is Err then returns this Err as is. If result is Ok then returns result of call await f(result.val). If result of call f() is null - returns Err('function returned null'). If result of call f() is Result - returns it as is. If call of f() throws exception, then returns Err.

Implementation

Future<Result<T2>> pipeAsync<T2>(Future Function(T) f) async {
  return then((resultT) async {
    if (resultT.isErr) {
      return Err<T2>(resultT.err);
    }
    var valueT = resultT.unwrap;
    var res = await f(valueT);
    if (res == null) {
      return Err<T2>(
          StateError('Result of f($valueT) == null, where `f` is $f'));
    } else {
      if (res is Error) {
        return Err<T2>(res);
      } else {
        if (res is Ok) {
          return Ok<T2>(res.val);
        } else if (res is Err) {
          return Err<T2>(res.err);
        } else {
          return Ok(res);
        }
      }
    }
  });
}