# wait<T> method

Future<List<T>> wait <T>(Iterable<Future<T>> futures, { bool eagerError: false, void cleanUp(T successValue) })

Waits for multiple futures to complete and collects their results.

Returns a future which will complete once all the provided futures have completed, either with their results, or with an error if either of the provided futures fail.

The value of the returned future will be a list of all the values that were produced.

If any future completes with an error, then the returned future completes with that error. If further futures also complete with errors, those errors are discarded.

If `eagerError` is true, the returned future completes with an error immediately on the first error from one of the futures. Otherwise all futures must complete before the returned future is completed (still with the first error; the remaining errors are silently dropped).

In the case of an error, `cleanUp` (if provided), is invoked on any non-null result of successful futures. This makes it possible to `cleanUp` resources that would otherwise be lost (since the returned future does not provide access to these values). The `cleanUp` function is unused if there is no error.

The call to `cleanUp` should not throw. If it does, the error will be an uncaught asynchronous error.

## Implementation

    static Future<List<T>> wait<T>(Iterable<Future<T>> futures,
        {bool eagerError: false, void cleanUp(T successValue)}) {
      final _Future<List<T>> result = new _Future<List<T>>();
      List<T> values; // Collects the values. Set to null on error.
      int remaining = 0; // How many futures are we waiting for.
      var error; // The first error from a future.
      StackTrace stackTrace; // The stackTrace that came with the error.

      // Handle an error from any of the futures.
      // TODO(jmesserly): use `void` return type once it can be inferred for the
      // `then` call below.
      handleError(theError, theStackTrace) {
        remaining--;
        if (values != null) {
          if (cleanUp != null) {
            for (var value in values) {
              if (value != null) {
                // Ensure errors from cleanUp are uncaught.
                new Future.sync(() {
                  cleanUp(value);
                });
              }
            }
          }
          values = null;
          if (remaining == 0 || eagerError) {
            result._completeError(theError, theStackTrace);
          } else {
            error = theError;
            stackTrace = theStackTrace;
          }
        } else if (remaining == 0 && !eagerError) {
          result._completeError(error, stackTrace);
        }
      }

      try {
        // As each future completes, put its value into the corresponding
        // position in the list of values.
        for (var future in futures) {
          int pos = remaining;
          future.then((T value) {
            remaining--;
            if (values != null) {
              values[pos] = value;
              if (remaining == 0) {
                result._completeWithValue(values);
              }
            } else {
              if (cleanUp != null && value != null) {
                // Ensure errors from cleanUp are uncaught.
                new Future.sync(() {
                  cleanUp(value);
                });
              }
              if (remaining == 0 && !eagerError) {
                result._completeError(error, stackTrace);
              }
            }
          }, onError: handleError);
          // Increment the 'remaining' after the call to 'then'.
          // If that call throws, we don't expect any future callback from
          // the future, and we also don't increment remaining.
          remaining++;
        }
        if (remaining == 0) {
          return new Future.value(const []);
        }
        values = new List<T>(remaining);
      } catch (e, st) {
        // The error must have been thrown while iterating over the futures
        // list, or while installing a callback handler on the future.
        if (remaining == 0 || eagerError) {
          // Throw a new Future.error.
          // Don't just call `result._completeError` since that would propagate
          // the error too eagerly, not giving the callers time to install
          // error handlers.
          // Also, don't use `_asyncCompleteError` since that one doesn't give
          // zones the chance to intercept the error.
          return new Future.error(e, st);
        } else {
          // Don't allocate a list for values, thus indicating that there was an
          // error.
          // Set error to the caught exception.
          error = e;
          stackTrace = st;
        }
      }
      return result;
    }