wait<T> static method

FutureOr<List<T>> wait<T>(
  1. Iterable<FutureOr<T>> values, {
  2. bool eagerError = false,
  3. void cleanUp(
    1. T
    )?,
})

Waits for multiple values to complete, collects their results, and returns them in the same order.

Implementation

static FutureOr<List<T>> wait<T>(
  Iterable<FutureOr<T>> values, {
  bool eagerError = false,
  void Function(T)? cleanUp,
}) {
  var hasFuture = false;

  for (final value in values) {
    if (value is Future<T>) {
      hasFuture = true;
      break;
    }
  }
  if (hasFuture) {
    return Future.wait(
      values.map((e) async => e),
      eagerError: eagerError,
      cleanUp: cleanUp,
    );
  } else {
    return values.cast<T>().toList();
  }
}