wait property

Future<List<T>> get wait

Waits for futures in parallel.

Waits for all the futures in this iterable. Returns a list of the resulting values, in the same order as the futures which created them, if all futures are successful.

Similar to Future.wait, but reports errors using a ParallelWaitError, which allows the caller to handle errors and dispose successful results if necessary.

The returned future is completed when all the futures have completed. If any of the futures do not complete, nor does the returned future.

If any future completes with an error, the returned future completes with a ParallelWaitError. The ParallelWaitError.values is a list of the values for successful futures and null for futures with errors. The ParallelWaitError.errors is a list of the same length, with null values for the successful futures and an AsyncError with the error for futures which completed with an error.

Implementation

Future<List<T>> get wait {
  var results = [for (var f in this) _FutureResult<T>(f)];
  if (results.isEmpty) return Future<List<T>>.value(<T>[]);

  @pragma('vm:awaiter-link')
  final c = Completer<List<T>>.sync();

  _FutureResult._waitAll(results, (errors) {
    if (errors == 0) {
      c.complete([for (var r in results) r.value]);
    } else {
      var errorList = [for (var r in results) r.errorOrNull];
      c.completeError(
        ParallelWaitError<List<T?>, List<AsyncError?>>(
          [for (var r in results) r.valueOrNull],
          errorList,
          errorCount: errors,
          defaultError: errorList.firstWhere(_notNull),
        ),
      );
    }
  });
  return c.future;
}