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

Side by Side Diff: corelib/src/future.dart

Issue 9860010: If any of the futures passed to Futures.wait leads to (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // Dart core library. 5 // Dart core library.
6 6
7 7
8 /** 8 /**
9 * A [Future] is used to obtain a value sometime in the future. Receivers of a 9 * A [Future] is used to obtain a value sometime in the future. Receivers of a
10 * [Future] obtain the value by passing a callback to [then]. For example: 10 * [Future] obtain the value by passing a callback to [then]. For example:
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 152
153 153
154 /** 154 /**
155 * [Futures] holds additional utility functions that operate on [Future]s (for 155 * [Futures] holds additional utility functions that operate on [Future]s (for
156 * example, waiting for a collection of Futures to complete). 156 * example, waiting for a collection of Futures to complete).
157 */ 157 */
158 class Futures { 158 class Futures {
159 159
160 /** 160 /**
161 * Returns a future which will complete once all the futures in a list are 161 * Returns a future which will complete once all the futures in a list are
162 * complete. (The value of the returned future will be a list of all the 162 * complete. If any of the futures in the list completes with an exception,
163 * values that were produced.) 163 * the resulting future also completes with an exception. (The value of the
164 * returned future will be a list of all the values that were produced.)
164 */ 165 */
165 static Future<List> wait(List<Future> futures) { 166 static Future<List> wait(List<Future> futures) {
167 if (futures.isEmpty()) {
168 return new Future<List>.immediate(const []);
169 }
170
166 Completer completer = new Completer<List>(); 171 Completer completer = new Completer<List>();
172 Future<List> result = completer.future;
167 int remaining = futures.length; 173 int remaining = futures.length;
168 List<Object> values = new List(futures.length); 174 List<Object> values = new List(futures.length);
169 175
170 // As each future completes, put its value into the corresponding 176 // As each future completes, put its value into the corresponding
171 // position in the list of values. 177 // position in the list of values.
172 for (int i = 0; i < futures.length; i++) { 178 for (int i = 0; i < futures.length; i++) {
173 // TODO(mattsh) - remove this after bug 179 // TODO(mattsh) - remove this after bug
174 // http://code.google.com/p/dart/issues/detail?id=333 is fixed. 180 // http://code.google.com/p/dart/issues/detail?id=333 is fixed.
175 int pos = i; 181 int pos = i;
176 futures[pos].then((Object value) { 182 Future future = futures[pos];
183 future.then((Object value) {
177 values[pos] = value; 184 values[pos] = value;
178 if (--remaining == 0) { 185 if (--remaining == 0 && !result.isComplete) {
179 completer.complete(values); 186 completer.complete(values);
180 } 187 }
181 }); 188 });
189 future.handleException((exception) {
190 if (!result.isComplete) completer.completeException(exception);
191 return true;
192 });
182 } 193 }
183 194 return result;
184 // Special case where all the futures are already completed,
185 // trigger the value now.
186 if (futures.length == 0) {
187 completer.complete(values);
188 }
189
190 return completer.future;
191 } 195 }
192 } 196 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698