resolveAsync method

Future<V> resolveAsync()

Resolves the computation asynchronously.

Always returns a Future that completes with the value (as V) or error.

If a resolution is in progress, returns the same Future. If already resolved, returns an already completed Future.

Implementation

Future<V> resolveAsync() {
  var result = _result;
  if (result != null) {
    var error = result.error;
    if (error != null) {
      var stackTrace = result.stackTrace;
      return Future.error(error, stackTrace);
    }
    return Future.value(result.value as V);
  }

  var future = _future;
  if (future != null) return future;

  var computer = _call ?? (throw StateError("Null `_call`"));
  future = _future = Future(computer);

  future.then(
    (value) {
      _result = (value: value, error: null, stackTrace: null);
      if (identical(future, _future)) {
        _future = null;
      }
      _call = null;
    },
    onError: (e, s) {
      _result = (value: null, error: e, stackTrace: s);
      if (identical(future, _future)) {
        _future = null;
      }
      _call = null;
    },
  );

  return future;
}