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 /// A type representing values that are either `Future<T>` or `T`. | 7 /// A type representing values that are either `Future<T>` or `T`. |
8 /// | 8 /// |
9 /// This class declaration is a public stand-in for an internal | 9 /// This class declaration is a public stand-in for an internal |
10 /// future-or-value generic type. References to this class are resolved to the | 10 /// future-or-value generic type. References to this class are resolved to the |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 * then the returned future completes with that error. | 329 * then the returned future completes with that error. |
330 * If further futures also complete with errors, those errors are discarded. | 330 * If further futures also complete with errors, those errors are discarded. |
331 * | 331 * |
332 * If `eagerError` is true, the returned future completes with an error | 332 * If `eagerError` is true, the returned future completes with an error |
333 * immediately on the first error from one of the futures. Otherwise all | 333 * immediately on the first error from one of the futures. Otherwise all |
334 * futures must complete before the returned future is completed (still with | 334 * futures must complete before the returned future is completed (still with |
335 * the first error; the remaining errors are silently dropped). | 335 * the first error; the remaining errors are silently dropped). |
336 * | 336 * |
337 * In the case of an error, [cleanUp] (if provided), is invoked on any | 337 * In the case of an error, [cleanUp] (if provided), is invoked on any |
338 * non-null result of successful futures. | 338 * non-null result of successful futures. |
339 * This makes it posible to `cleanUp` resources that would otherwise be | 339 * This makes it possible to `cleanUp` resources that would otherwise be |
340 * lost (since the returned future does not provide access to these values). | 340 * lost (since the returned future does not provide access to these values). |
341 * The [cleanUp] function is unused if there is no error. | 341 * The [cleanUp] function is unused if there is no error. |
342 * | 342 * |
343 * The call to [cleanUp] should not throw. If it does, the error will be an | 343 * The call to [cleanUp] should not throw. If it does, the error will be an |
344 * uncaught asynchronous error. | 344 * uncaught asynchronous error. |
345 */ | 345 */ |
346 static Future<List<T>> wait<T>(Iterable<Future<T>> futures, | 346 static Future<List<T>> wait<T>(Iterable<Future<T>> futures, |
347 {bool eagerError: false, void cleanUp(T successValue)}) { | 347 {bool eagerError: false, void cleanUp(T successValue)}) { |
348 final _Future<List<T>> result = new _Future<List<T>>(); | 348 final _Future<List<T>> result = new _Future<List<T>>(); |
349 List<T> values; // Collects the values. Set to null on error. | 349 List<T> values; // Collects the values. Set to null on error. |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 return true; | 486 return true; |
487 }); | 487 }); |
488 } | 488 } |
489 | 489 |
490 // Constant `true` function, used as callback by [forEach]. | 490 // Constant `true` function, used as callback by [forEach]. |
491 static bool _kTrue(_) => true; | 491 static bool _kTrue(_) => true; |
492 | 492 |
493 /** | 493 /** |
494 * Performs an operation repeatedly until it returns `false`. | 494 * Performs an operation repeatedly until it returns `false`. |
495 * | 495 * |
496 * The operation, [f], may be either synchronous or asynchronous. | 496 * The operation, [action], may be either synchronous or asynchronous. |
497 * | 497 * |
498 * The operation is called repeatedly as long as it returns either the [bool] | 498 * The operation is called repeatedly as long as it returns either the [bool] |
499 * value `true` or a `Future<bool>` which completes with the value `true`. | 499 * value `true` or a `Future<bool>` which completes with the value `true`. |
500 * | 500 * |
501 * If a call to [f] returns `false` or a [Future] that completes to `false`, | 501 * If a call to [action] returns `false` or a [Future] that completes to |
502 * iteration ends and the future returned by [doWhile] is completed with | 502 * `false`, iteration ends and the future returned by [doWhile] is completed |
503 * a `null` value. | 503 * with a `null` value. |
504 * | 504 * |
505 * If a call to [f] throws or a future returned by [f] completes with | 505 * If a call to [action] throws or a future returned by [action] completes |
506 * an error, iteration ends and the future returned by [doWhile] | 506 * with an error, iteration ends and the future returned by [doWhile] |
507 * completes with the same error. | 507 * completes with the same error. |
508 * | 508 * |
509 * Calls to [action] may happen at any time, | 509 * Calls to [action] may happen at any time, |
510 * including immediately after calling `doWhile`. | 510 * including immediately after calling `doWhile`. |
511 * The only restriction is a new call to [action] won't happen before | 511 * The only restriction is a new call to [action] won't happen before |
512 * the previous call has returned, and if it returned a `Future<bool>`, not | 512 * the previous call has returned, and if it returned a `Future<bool>`, not |
513 * until that future has completed. | 513 * until that future has completed. |
514 */ | 514 */ |
515 static Future doWhile(FutureOr<bool> action()) { | 515 static Future doWhile(FutureOr<bool> action()) { |
516 _Future doneSignal = new _Future(); | 516 _Future doneSignal = new _Future(); |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
909 AsyncError replacement = Zone.current.errorCallback(error, stackTrace); | 909 AsyncError replacement = Zone.current.errorCallback(error, stackTrace); |
910 if (replacement != null) { | 910 if (replacement != null) { |
911 error = _nonNullError(replacement.error); | 911 error = _nonNullError(replacement.error); |
912 stackTrace = replacement.stackTrace; | 912 stackTrace = replacement.stackTrace; |
913 } | 913 } |
914 result._asyncCompleteError(error, stackTrace); | 914 result._asyncCompleteError(error, stackTrace); |
915 } | 915 } |
916 | 916 |
917 /** Helper function that converts `null` to a [NullThrownError]. */ | 917 /** Helper function that converts `null` to a [NullThrownError]. */ |
918 Object _nonNullError(Object error) => error ?? new NullThrownError(); | 918 Object _nonNullError(Object error) => error ?? new NullThrownError(); |
OLD | NEW |