Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2834)

Unified Diff: dart/sdk/lib/async/future_impl.dart

Issue 12296011: Version 0.3.7.4 (Closed) Base URL: http://dart.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: dart/sdk/lib/async/future_impl.dart
===================================================================
--- dart/sdk/lib/async/future_impl.dart (revision 18634)
+++ dart/sdk/lib/async/future_impl.dart (working copy)
@@ -112,32 +112,35 @@
}
factory _FutureImpl.wait(Iterable<Future> futures) {
- // TODO(ajohnsen): can we do better wrt. the generic type T?
- if (futures.isEmpty) {
- return new Future<List>.immediate(const []);
+ Completer completer;
+ // List collecting values from the futures.
+ // Set to null if an error occurs.
+ List values;
+ void handleError(error) {
+ if (values != null) {
+ values = null;
+ completer.completeError(error.error, error.stackTrace);
+ }
}
-
- Completer completer = new Completer<List>();
- int remaining = futures.length;
- List values = new List.fixedLength(futures.length);
-
// As each future completes, put its value into the corresponding
// position in the list of values.
- int i = 0;
- bool completed = false;
+ int remaining = 0;
for (Future future in futures) {
- int pos = i++;
- future.then((Object value) {
+ int pos = remaining++;
+ future.catchError(handleError).then((Object value) {
+ if (values == null) return null;
values[pos] = value;
- if (--remaining == 0) {
+ remaining--;
+ if (remaining == 0) {
completer.complete(values);
}
- }).catchError((error) {
- if (!completed) completer.completeError(error.error, error.stackTrace);
- completed = true;
});
}
-
+ if (remaining == 0) {
+ return new Future.immediate(const []);
+ }
+ values = new List.fixedLength(remaining);
+ completer = new Completer<List>();
return completer.future;
}
« no previous file with comments | « dart/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart ('k') | dart/sdk/lib/async/stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698