captureAll<T> method

Future<List<Result<T>>> captureAll <T>(Iterable<FutureOr<T>> elements)

Captures each future in elements,

Returns a (future of) a list of results for each element in elements, in iteration order. Each future in elements is captured and each non-future is wrapped as a Result.value. The returned future will never have an error.

Implementation

static Future<List<Result<T>>> captureAll<T>(Iterable<FutureOr<T>> elements) {
  var results = <Result<T>>[];
  int pending = 0;
  Completer<List<Result<T>>> completer;
  for (var element in elements) {
    if (element is Future<T>) {
      int i = results.length;
      results.add(null);
      pending++;
      Result.capture<T>(element).then((result) {
        results[i] = result;
        if (--pending == 0) {
          completer.complete(results);
        }
      });
    } else {
      results.add(new Result<T>.value(element));
    }
  }
  if (pending == 0) {
    return new Future<List<Result<T>>>.value(results);
  }
  completer = new Completer<List<Result<T>>>();
  return completer.future;
}