| 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 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 }, onError: handleError); | 310 }, onError: handleError); |
| 311 } | 311 } |
| 312 if (remaining == 0) { | 312 if (remaining == 0) { |
| 313 return new Future.value(const []); | 313 return new Future.value(const []); |
| 314 } | 314 } |
| 315 values = new List(remaining); | 315 values = new List(remaining); |
| 316 return result; | 316 return result; |
| 317 } | 317 } |
| 318 | 318 |
| 319 /** | 319 /** |
| 320 * Returns the result of the first future in [futures] to complete. |
| 321 * |
| 322 * The returned future is completed with the result of the first |
| 323 * future in [futures] to report that it is complete. |
| 324 * The results of all the other futures are discarded. |
| 325 * |
| 326 * If [futures] is empty, or if none of its futures complete, |
| 327 * the returned future never completes. |
| 328 */ |
| 329 static Future/*<T>*/ any/*<T>*/(Iterable<Future/*<T>*/> futures) { |
| 330 var completer = new Completer.sync(); |
| 331 var onValue = (value) { |
| 332 if (!completer.isCompleted) completer.complete(value); |
| 333 }; |
| 334 var onError = (error, stack) { |
| 335 if (!completer.isCompleted) completer.completeError(error, stack); |
| 336 }; |
| 337 for (var future in futures) { |
| 338 future.then(onValue, onError: onError); |
| 339 } |
| 340 return completer.future; |
| 341 } |
| 342 |
| 343 |
| 344 /** |
| 320 * Perform an async operation for each element of the iterable, in turn. | 345 * Perform an async operation for each element of the iterable, in turn. |
| 321 * | 346 * |
| 322 * Runs [f] for each element in [input] in order, moving to the next element | 347 * Runs [f] for each element in [input] in order, moving to the next element |
| 323 * only when the [Future] returned by [f] completes. Returns a [Future] that | 348 * only when the [Future] returned by [f] completes. Returns a [Future] that |
| 324 * completes when all elements have been processed. | 349 * completes when all elements have been processed. |
| 325 * | 350 * |
| 326 * The return values of all [Future]s are discarded. Any errors will cause the | 351 * The return values of all [Future]s are discarded. Any errors will cause the |
| 327 * iteration to stop and will be piped through the returned [Future]. | 352 * iteration to stop and will be piped through the returned [Future]. |
| 328 * | 353 * |
| 329 * If [f] returns a non-[Future], iteration continues immediately. Otherwise | 354 * If [f] returns a non-[Future], iteration continues immediately. Otherwise |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 if (replacement != null) { | 745 if (replacement != null) { |
| 721 error = _nonNullError(replacement.error); | 746 error = _nonNullError(replacement.error); |
| 722 stackTrace = replacement.stackTrace; | 747 stackTrace = replacement.stackTrace; |
| 723 } | 748 } |
| 724 result._completeError(error, stackTrace); | 749 result._completeError(error, stackTrace); |
| 725 } | 750 } |
| 726 | 751 |
| 727 /** Helper function that converts `null` to a [NullThrownError]. */ | 752 /** Helper function that converts `null` to a [NullThrownError]. */ |
| 728 Object _nonNullError(Object error) => | 753 Object _nonNullError(Object error) => |
| 729 (error != null) ? error : new NullThrownError(); | 754 (error != null) ? error : new NullThrownError(); |
| OLD | NEW |