| 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 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 */ | 356 */ |
| 357 static Future forEach(Iterable input, f(element)) { | 357 static Future forEach(Iterable input, f(element)) { |
| 358 Iterator iterator = input.iterator; | 358 Iterator iterator = input.iterator; |
| 359 return doWhile(() { | 359 return doWhile(() { |
| 360 if (!iterator.moveNext()) return false; | 360 if (!iterator.moveNext()) return false; |
| 361 return new Future.sync(() => f(iterator.current)).then((_) => true); | 361 return new Future.sync(() => f(iterator.current)).then((_) => true); |
| 362 }); | 362 }); |
| 363 } | 363 } |
| 364 | 364 |
| 365 /** | 365 /** |
| 366 * Perform an async operation repeatedly until it returns `false`. | 366 * Performs an async operation repeatedly until it returns `false`. |
| 367 * | 367 * |
| 368 * Runs [f] repeatedly, starting the next iteration only when the [Future] | 368 * The function [f] is called repeatedly while it returns either the [bool] |
| 369 * returned by [f] completes to `true`. Returns a [Future] that completes once | 369 * value `true` or a [Future] which completes with the value `true`. |
| 370 * [f] returns `false`. | |
| 371 * | 370 * |
| 372 * The return values of all [Future]s are discarded. Any errors will cause the | 371 * If a call to [f] returns `false` or a [Future] that completes to `false`, |
| 373 * iteration to stop and will be piped through the returned [Future]. | 372 * iteration ends and the future returned by [doWhile] is completed. |
| 374 * | 373 * |
| 375 * The function [f] may return either a [bool] or a [Future] that completes to | 374 * If a future returned by [f] completes with an error, iteration ends and |
| 376 * a [bool]. If it returns a non-[Future], iteration continues immediately. | 375 * the future returned by [doWhile] completes with the same error. |
| 377 * Otherwise it waits for the returned [Future] to complete. | 376 * |
| 377 * The [f] function must return either a `bool` value or a [Future] completing |
| 378 * with a `bool` value. |
| 378 */ | 379 */ |
| 379 static Future doWhile(f()) { | 380 static Future doWhile(f()) { |
| 380 _Future doneSignal = new _Future(); | 381 _Future doneSignal = new _Future(); |
| 381 var nextIteration; | 382 var nextIteration; |
| 382 // Bind this callback explicitly so that each iteration isn't bound in the | 383 // Bind this callback explicitly so that each iteration isn't bound in the |
| 383 // context of all the previous iterations' callbacks. | 384 // context of all the previous iterations' callbacks. |
| 384 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) { | 385 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) { |
| 385 if (keepGoing) { | 386 if (keepGoing) { |
| 386 new Future.sync(f).then(nextIteration, | 387 new Future.sync(f).then(nextIteration, |
| 387 onError: doneSignal._completeError); | 388 onError: doneSignal._completeError); |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 755 if (replacement != null) { | 756 if (replacement != null) { |
| 756 error = _nonNullError(replacement.error); | 757 error = _nonNullError(replacement.error); |
| 757 stackTrace = replacement.stackTrace; | 758 stackTrace = replacement.stackTrace; |
| 758 } | 759 } |
| 759 result._completeError(error, stackTrace); | 760 result._completeError(error, stackTrace); |
| 760 } | 761 } |
| 761 | 762 |
| 762 /** Helper function that converts `null` to a [NullThrownError]. */ | 763 /** Helper function that converts `null` to a [NullThrownError]. */ |
| 763 Object _nonNullError(Object error) => | 764 Object _nonNullError(Object error) => |
| 764 (error != null) ? error : new NullThrownError(); | 765 (error != null) ? error : new NullThrownError(); |
| OLD | NEW |