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

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

Issue 2252823004: Deal with synchronous errors in Future.wait. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Update changelog. Created 4 years, 4 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: sdk/lib/async/future.dart
diff --git a/sdk/lib/async/future.dart b/sdk/lib/async/future.dart
index b4d9c243393cb33fd47e1be15d4f25e71c8961fe..0eb4b6ec4940c2191406a38907476ae1cedb2868 100644
--- a/sdk/lib/async/future.dart
+++ b/sdk/lib/async/future.dart
@@ -287,32 +287,52 @@ abstract class Future<T> {
}
}
- // As each future completes, put its value into the corresponding
- // position in the list of values.
- for (Future future in futures) {
- int pos = remaining++;
- future.then((Object/*=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);
+ try {
+ // As each future completes, put its value into the corresponding
+ // position in the list of values.
+ for (Future future in futures) {
+ int pos = remaining;
+ future.then((Object/*=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);
- }
- if (remaining == 0) {
- return new Future.value(const []);
+ }, 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 since the increment is after the call we won't
+ // reach the line).
Lasse Reichstein Nielsen 2016/08/18 11:15:08 change parenthesized comment to: , and we also don
floitsch 2016/08/18 11:42:55 Done.
+ 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) {
+ // Just complete the error ourselves.
Lasse Reichstein Nielsen 2016/08/18 11:15:08 ourselves -> immediately.
floitsch 2016/08/18 11:42:55 Done.
+ result._completeError(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;
+ }
}
- values = new List/*<T>*/(remaining);
return result;
}
« no previous file with comments | « CHANGELOG.md ('k') | tests/lib/async/future_test.dart » ('j') | tests/lib/async/future_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698