OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart.async; | 5 part of dart.async; |
6 | 6 |
7 /** | 7 /** |
8 * An object representing a delayed computation. | 8 * An object representing a delayed computation. |
9 * | 9 * |
10 * A [Future] is used to represent a potential value, or error, | 10 * A [Future] is used to represent a potential value, or error, |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 result._completeError(theError, theStackTrace); | 280 result._completeError(theError, theStackTrace); |
281 } else { | 281 } else { |
282 error = theError; | 282 error = theError; |
283 stackTrace = theStackTrace; | 283 stackTrace = theStackTrace; |
284 } | 284 } |
285 } else if (remaining == 0 && !eagerError) { | 285 } else if (remaining == 0 && !eagerError) { |
286 result._completeError(error, stackTrace); | 286 result._completeError(error, stackTrace); |
287 } | 287 } |
288 } | 288 } |
289 | 289 |
290 // As each future completes, put its value into the corresponding | 290 try { |
291 // position in the list of values. | 291 // As each future completes, put its value into the corresponding |
292 for (Future future in futures) { | 292 // position in the list of values. |
293 int pos = remaining++; | 293 for (Future future in futures) { |
294 future.then((Object/*=T*/ value) { | 294 int pos = remaining; |
295 remaining--; | 295 future.then((Object/*=T*/ value) { |
296 if (values != null) { | 296 remaining--; |
297 values[pos] = value; | 297 if (values != null) { |
298 if (remaining == 0) { | 298 values[pos] = value; |
299 result._completeWithValue(values); | 299 if (remaining == 0) { |
300 result._completeWithValue(values); | |
301 } | |
302 } else { | |
303 if (cleanUp != null && value != null) { | |
304 // Ensure errors from cleanUp are uncaught. | |
305 new Future.sync(() { cleanUp(value); }); | |
306 } | |
307 if (remaining == 0 && !eagerError) { | |
308 result._completeError(error, stackTrace); | |
309 } | |
300 } | 310 } |
301 } else { | 311 }, onError: handleError); |
302 if (cleanUp != null && value != null) { | 312 // Increment the 'remaining' after the call to 'then'. |
303 // Ensure errors from cleanUp are uncaught. | 313 // If that call throws, we don't expect any future callback from |
304 new Future.sync(() { cleanUp(value); }); | 314 // the future (and since the increment is after the call we won't |
305 } | 315 // 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.
| |
306 if (remaining == 0 && !eagerError) { | 316 remaining++; |
307 result._completeError(error, stackTrace); | 317 } |
308 } | 318 if (remaining == 0) { |
309 } | 319 return new Future.value(const []); |
310 }, onError: handleError); | 320 } |
321 values = new List/*<T>*/(remaining); | |
322 } catch (e, st) { | |
323 // The error must have been thrown while iterating over the futures | |
324 // list, or while installing a callback handler on the future. | |
325 if (remaining == 0 || eagerError) { | |
326 // Just complete the error ourselves. | |
Lasse Reichstein Nielsen
2016/08/18 11:15:08
ourselves -> immediately.
floitsch
2016/08/18 11:42:55
Done.
| |
327 result._completeError(e, st); | |
328 } else { | |
329 // Don't allocate a list for values, thus indicating that there was an | |
330 // error. | |
331 // Set error to the caught exception. | |
332 error = e; | |
333 stackTrace = st; | |
334 } | |
311 } | 335 } |
312 if (remaining == 0) { | |
313 return new Future.value(const []); | |
314 } | |
315 values = new List/*<T>*/(remaining); | |
316 return result; | 336 return result; |
317 } | 337 } |
318 | 338 |
319 /** | 339 /** |
320 * Returns the result of the first future in [futures] to complete. | 340 * Returns the result of the first future in [futures] to complete. |
321 * | 341 * |
322 * The returned future is completed with the result of the first | 342 * The returned future is completed with the result of the first |
323 * future in [futures] to report that it is complete. | 343 * future in [futures] to report that it is complete. |
324 * The results of all the other futures are discarded. | 344 * The results of all the other futures are discarded. |
325 * | 345 * |
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
756 if (replacement != null) { | 776 if (replacement != null) { |
757 error = _nonNullError(replacement.error); | 777 error = _nonNullError(replacement.error); |
758 stackTrace = replacement.stackTrace; | 778 stackTrace = replacement.stackTrace; |
759 } | 779 } |
760 result._completeError(error, stackTrace); | 780 result._completeError(error, stackTrace); |
761 } | 781 } |
762 | 782 |
763 /** Helper function that converts `null` to a [NullThrownError]. */ | 783 /** Helper function that converts `null` to a [NullThrownError]. */ |
764 Object _nonNullError(Object error) => | 784 Object _nonNullError(Object error) => |
765 (error != null) ? error : new NullThrownError(); | 785 (error != null) ? error : new NullThrownError(); |
OLD | NEW |