resolveAsync method

Future<V> resolveAsync({
  1. bool throwError = true,
  2. Future<V> onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
  3. V? onErrorValue,
})

Resolves the computation asynchronously.

Always returns a Future<V> that completes with the resolved value or completes with an error.

  • If a resolution is in progress, returns the same in-flight Future.
  • If already resolved, returns an already completed Future.
  • When the computation completes (success or failure), onCompute is invoked with the resulting value or error.

Parameters:

  • throwError: When true (default), the returned Future completes with the original error and StackTrace. When false, errors are handled by onError or onErrorValue.

  • onError: Optional asynchronous error handler used only when throwError is false. Receives the error object and its StackTrace and must return a Future<V> whose value becomes the resolution result.

  • onErrorValue: Fallback value (default null) used to complete the returned Future when an error occurs, throwError is false, and onError is not provided.

Implementation

Future<V> resolveAsync({
  bool throwError = true,
  Future<V> Function(Object error, StackTrace stackTrace)? onError,
  V? onErrorValue,
}) {
  var result = _result;
  if (result != null) {
    var error = result.error;
    if (error != null) {
      var stackTrace = result.stackTrace!;
      if (throwError) {
        return Future.error(error, stackTrace);
      } else if (onError != null) {
        return onError(error, stackTrace);
      } else {
        return Future.value(onErrorValue ?? (null as V));
      }
    }
    return Future.value(result.value as V);
  }

  var future = _future;
  if (future != null) {
    if (!throwError) {
      if (onError != null) {
        return future.catchError(onError);
      } else {
        return future.catchError((e, s) => _castErrorValue(onErrorValue));
      }
    }
    return future;
  }

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

  final posCompute = this.posCompute;
  if (posCompute != null) {
    future = _future = future.then((v) {
      return posCompute(v, null, null);
    }, onError: (e, s) {
      return posCompute(null, e, s);
    });
  }

  return _resolveFuture(future, throwError, onError, onErrorValue);
}