Chromium Code Reviews| Index: sdk/lib/async/future.dart |
| diff --git a/sdk/lib/async/future.dart b/sdk/lib/async/future.dart |
| index 5240bf8d5d707207210f6f980946e7e6b3459d93..d5c65f67a83485166b5c9451650ee5e81c358306 100644 |
| --- a/sdk/lib/async/future.dart |
| +++ b/sdk/lib/async/future.dart |
| @@ -423,14 +423,19 @@ abstract class Future<T> { |
| } |
| /** |
| - * Perform an async operation for each element of the iterable, in turn. |
| + * Perform an operation for each element of the iterable, in turn. |
| * |
| - * Runs [f] for each element in [input] in order, moving to the next element |
| - * only when the [Future] returned by [f] completes. Returns a [Future] that |
| - * completes when all elements have been processed. |
| + * The operation, [f], may be either synchronous or asynchronous. |
| * |
| - * The return values of all [Future]s are discarded. Any errors will cause the |
| - * iteration to stop and will be piped through the returned [Future]. |
| + * Calls [f] with each element in [input] in order, moving to the next element |
| + * only when the call returns, and if [f] returns a `Future<T>`, when |
|
floitsch
2017/04/05 13:19:25
in order. If [f] returns a [Future<T>], waits unti
Lasse Reichstein Nielsen
2017/04/06 09:49:27
Done.
|
| + * that future has completed. |
| + * Returns a [Future] that completes with `null` when all elements have been |
|
floitsch
2017/04/05 13:19:25
Make this a separate paragraph.
Lasse Reichstein Nielsen
2017/04/06 09:49:27
Done.
|
| + * processed. |
| + * |
| + * The return values of the calls are discarded. |
|
Lasse Reichstein Nielsen
2017/04/06 09:49:27
Reworded slightly.
|
| + * Any error from [f], synchronous or asynchronous, will stop the iteration |
|
floitsch
2017/04/05 13:19:25
Maybe make this a paragraph.
Lasse Reichstein Nielsen
2017/04/06 09:49:27
Done.
|
| + * and will be reported in the returned [Future]. |
| * |
| * If [f] returns a non-[Future], iteration continues immediately. Otherwise |
| * it waits for the returned [Future] to complete. |
|
Lasse Reichstein Nielsen
2017/04/06 09:49:27
This paragraph is redundant with the second paragr
|
| @@ -439,15 +444,22 @@ abstract class Future<T> { |
| var iterator = input.iterator; |
| return doWhile(() { |
| if (!iterator.moveNext()) return false; |
| - return new Future.sync(() => f(iterator.current)).then((_) => true); |
| + var result = f(iterator.current); |
| + if (result is Future<T>) return result.then(_kTrue); |
| + return true; |
| }); |
| } |
| + // Constant `true` function, used as callback by [forEach]. |
| + static bool _kTrue(_) => true; |
| + |
| /** |
| - * Performs an async operation repeatedly until it returns `false`. |
| + * Performs an operation repeatedly until it returns `false`. |
| + * |
| + * The operation, [f], may be either synchronous or asynchronous. |
| * |
| - * The function [f] is called repeatedly while it returns either the [bool] |
| - * value `true` or a [Future] which completes with the value `true`. |
| + * The operation is called repeatedly as long as it returns either the [bool] |
| + * value `true` or a `Future<bool>` which completes with the value `true`. |
| * |
| * If a call to [f] returns `false` or a [Future] that completes to `false`, |
| * iteration ends and the future returned by [doWhile] is completed with |
| @@ -456,6 +468,11 @@ abstract class Future<T> { |
| * If a call to [f] throws or a future returned by [f] completes with |
| * an error, iteration ends and the future returned by [doWhile] |
| * completes with the same error. |
| + * |
| + * Calls to [f] may happen at any time, including immediately after calling |
| + * `doWhile`. The only restriction is a new call to [f] won't happen before |
| + * the previous call has returned, and if it returned a `Future<bool>`, not |
| + * until that future has completed. |
| */ |
| static Future doWhile(FutureOr<bool> f()) { |
| _Future doneSignal = new _Future(); |
| @@ -464,8 +481,18 @@ abstract class Future<T> { |
| // context of all the previous iterations' callbacks. |
| nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) { |
| if (keepGoing) { |
| - new Future.sync(f) |
| - .then(nextIteration, onError: doneSignal._completeError); |
| + FutureOr<bool> result; |
| + try { |
| + result = f(); |
| + } catch (error, stackTrace) { |
| + _completeWithErrorCallback(doneSignal, error, stackTrace); |
| + return; |
| + } |
| + if (result is Future<bool>) { |
| + result.then(nextIteration, onError: doneSignal._completeError); |
| + return; |
| + } |
| + nextIteration(result); |
| } else { |
| doneSignal._complete(null); |
| } |