| 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 we also don't increment remaining. |
| 305 } | 315 remaining++; |
| 306 if (remaining == 0 && !eagerError) { | 316 } |
| 307 result._completeError(error, stackTrace); | 317 if (remaining == 0) { |
| 308 } | 318 return new Future.value(const []); |
| 309 } | 319 } |
| 310 }, onError: handleError); | 320 values = new List/*<T>*/(remaining); |
| 321 } catch (e, st) { |
| 322 // The error must have been thrown while iterating over the futures |
| 323 // list, or while installing a callback handler on the future. |
| 324 if (remaining == 0 || eagerError) { |
| 325 // Just complete the error immediately. |
| 326 result._completeError(e, st); |
| 327 } else { |
| 328 // Don't allocate a list for values, thus indicating that there was an |
| 329 // error. |
| 330 // Set error to the caught exception. |
| 331 error = e; |
| 332 stackTrace = st; |
| 333 } |
| 311 } | 334 } |
| 312 if (remaining == 0) { | |
| 313 return new Future.value(const []); | |
| 314 } | |
| 315 values = new List/*<T>*/(remaining); | |
| 316 return result; | 335 return result; |
| 317 } | 336 } |
| 318 | 337 |
| 319 /** | 338 /** |
| 320 * Returns the result of the first future in [futures] to complete. | 339 * Returns the result of the first future in [futures] to complete. |
| 321 * | 340 * |
| 322 * The returned future is completed with the result of the first | 341 * The returned future is completed with the result of the first |
| 323 * future in [futures] to report that it is complete. | 342 * future in [futures] to report that it is complete. |
| 324 * The results of all the other futures are discarded. | 343 * The results of all the other futures are discarded. |
| 325 * | 344 * |
| (...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 if (replacement != null) { | 775 if (replacement != null) { |
| 757 error = _nonNullError(replacement.error); | 776 error = _nonNullError(replacement.error); |
| 758 stackTrace = replacement.stackTrace; | 777 stackTrace = replacement.stackTrace; |
| 759 } | 778 } |
| 760 result._completeError(error, stackTrace); | 779 result._completeError(error, stackTrace); |
| 761 } | 780 } |
| 762 | 781 |
| 763 /** Helper function that converts `null` to a [NullThrownError]. */ | 782 /** Helper function that converts `null` to a [NullThrownError]. */ |
| 764 Object _nonNullError(Object error) => | 783 Object _nonNullError(Object error) => |
| 765 (error != null) ? error : new NullThrownError(); | 784 (error != null) ? error : new NullThrownError(); |
| OLD | NEW |