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

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

Issue 12295009: Recommit changing List.skip/take/revert returns Iterable and remove mappedBy. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Make Future.wait simpler. 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
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/lib/js_array.dart ('k') | sdk/lib/async/stream.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/async/future_impl.dart
diff --git a/sdk/lib/async/future_impl.dart b/sdk/lib/async/future_impl.dart
index 8df3d4e612fdfc016a166ebe0cd2f4a8bec3d428..85d73af9fa37a6476ea871bef81c4e036527e44d 100644
--- a/sdk/lib/async/future_impl.dart
+++ b/sdk/lib/async/future_impl.dart
@@ -112,32 +112,35 @@ class _FutureImpl<T> implements Future<T> {
}
factory _FutureImpl.wait(Iterable<Future> futures) {
Lasse Reichstein Nielsen 2013/02/18 09:59:29 This implementation of wait is slightly safer than
- // 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 | « sdk/lib/_internal/compiler/implementation/lib/js_array.dart ('k') | sdk/lib/async/stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698