| Index: sdk/lib/async/future.dart
|
| diff --git a/sdk/lib/async/future.dart b/sdk/lib/async/future.dart
|
| index c14318b9d0e39fecd86b73fc8919d0bfb5d09e5c..3d0a3c7b2f932956015da1b1cc7a9ea360076e7a 100644
|
| --- a/sdk/lib/async/future.dart
|
| +++ b/sdk/lib/async/future.dart
|
| @@ -227,26 +227,28 @@ class Futures {
|
| * the resulting future also completes with an exception. (The value of the
|
| * returned future will be a list of all the values that were produced.)
|
| */
|
| - static Future<List> wait(List<Future> futures) {
|
| + static Future<List> wait(Iterable<Future> futures) {
|
| if (futures.isEmpty) {
|
| return new Future<List>.immediate(const []);
|
| }
|
|
|
| Completer completer = new Completer<List>();
|
| Future<List> result = completer.future;
|
| - int remaining = futures.length;
|
| - List values = new List(futures.length);
|
| + int remaining = 0;
|
| + int seen = 0;
|
| + bool initializing = true;
|
| + List values = [];
|
|
|
| // As each future completes, put its value into the corresponding
|
| // position in the list of values.
|
| - for (int i = 0; i < futures.length; i++) {
|
| - // TODO(mattsh) - remove this after bug
|
| - // http://code.google.com/p/dart/issues/detail?id=333 is fixed.
|
| - int pos = i;
|
| - Future future = futures[pos];
|
| + for (Future future in futures) {
|
| + int pos = seen;
|
| + remaining++;
|
| + seen++;
|
| + values.length = seen;
|
| future.then((Object value) {
|
| values[pos] = value;
|
| - if (--remaining == 0 && !result.isComplete) {
|
| + if (--remaining == 0 && !result.isComplete && !initializing) {
|
| completer.complete(values);
|
| }
|
| });
|
| @@ -257,6 +259,11 @@ class Futures {
|
| return true;
|
| });
|
| }
|
| + initializing = false;
|
| + if (remaining == 0 && !result.isComplete) {
|
| + // All futures already had their values set.
|
| + completer.complete(values);
|
| + }
|
| return result;
|
| }
|
| }
|
|
|