Chromium Code Reviews| 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 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 | 284 |
| 285 /** | 285 /** |
| 286 * Perform an async operation for each element of the iterable, in turn. | 286 * Perform an async operation for each element of the iterable, in turn. |
| 287 * | 287 * |
| 288 * Runs [f] for each element in [input] in order, moving to the next element | 288 * Runs [f] for each element in [input] in order, moving to the next element |
| 289 * only when the [Future] returned by [f] completes. Returns a [Future] that | 289 * only when the [Future] returned by [f] completes. Returns a [Future] that |
| 290 * completes when all elements have been processed. | 290 * completes when all elements have been processed. |
| 291 * | 291 * |
| 292 * The return values of all [Future]s are discarded. Any errors will cause the | 292 * The return values of all [Future]s are discarded. Any errors will cause the |
| 293 * iteration to stop and will be piped through the returned [Future]. | 293 * iteration to stop and will be piped through the returned [Future]. |
| 294 * | |
| 295 * [f] may return a [Future] or a non-[Future] value. | |
|
Lasse Reichstein Nielsen
2014/06/26 06:26:49
Add "The function " first, to not begin the senten
nweiz
2014/06/26 19:48:11
Done.
| |
| 294 */ | 296 */ |
| 295 static Future forEach(Iterable input, Future f(element)) { | 297 static Future forEach(Iterable input, f(element)) { |
| 298 Iterator iterator = input.iterator; | |
| 299 return doWhile(() { | |
| 300 if (!iterator.moveNext()) return false; | |
| 301 return new Future.sync(() => f(iterator.current)).then((_) => true); | |
|
Lasse Reichstein Nielsen
2014/06/26 06:26:49
Maybe:
return new Future.sync(() { f(iterator.cu
nweiz
2014/06/26 19:48:11
That doesn't handle the case where f returns a Fut
| |
| 302 }); | |
| 303 } | |
| 304 | |
| 305 /** | |
| 306 * Perform an async operation repeatedly until it returns `false`. | |
| 307 * | |
| 308 * Runs [f] repeatedly, starting the next iteration only when the [Future] | |
| 309 * returned by [f] completes to `true`. Returns a [Future] that completes once | |
| 310 * [f] returns `false`. | |
| 311 * | |
| 312 * The return values of all [Future]s are discarded. Any errors will cause the | |
| 313 * iteration to stop and will be piped through the returned [Future]. | |
| 314 * | |
| 315 * [f] may return a [Future<bool>] or a non-[Future] [bool]. | |
|
Lasse Reichstein Nielsen
2014/06/26 06:26:49
It can return Future<dynamic> or Future<Object> as
nweiz
2014/06/26 19:48:11
Done.
| |
| 316 */ | |
| 317 static Future doWhile(f()) { | |
| 296 _Future doneSignal = new _Future(); | 318 _Future doneSignal = new _Future(); |
| 297 Iterator iterator = input.iterator; | 319 var nextIteration; |
| 298 void nextElement(_) { | 320 // Bind this callback explicitly so that each iteration isn't bound in the |
| 299 if (iterator.moveNext()) { | 321 // context of all the previous iterations' callbacks. |
| 300 new Future.sync(() => f(iterator.current)) | 322 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) { |
| 301 .then(nextElement, onError: doneSignal._completeError); | 323 if (keepGoing) { |
| 324 new Future.sync(f).then(nextIteration, | |
| 325 onError: doneSignal._completeError); | |
|
Lasse Reichstein Nielsen
2014/06/26 06:26:49
Maybe indent this line to the '('?
nweiz
2014/06/26 19:48:11
Done.
| |
| 302 } else { | 326 } else { |
| 303 doneSignal._complete(null); | 327 doneSignal._complete(null); |
| 304 } | 328 } |
| 305 } | 329 }, runGuarded: true); |
| 306 nextElement(null); | 330 nextIteration(keepGoing); |
|
Lasse Reichstein Nielsen
2014/06/26 06:26:49
keepGoing -> true ?
Tests? :)
nweiz
2014/06/26 19:48:11
Done.
| |
| 307 return doneSignal; | 331 return doneSignal; |
| 308 } | 332 } |
| 309 | 333 |
| 310 /** | 334 /** |
| 311 * Register callbacks to be called when this future completes. | 335 * Register callbacks to be called when this future completes. |
| 312 * | 336 * |
| 313 * When this future completes with a value, | 337 * When this future completes with a value, |
| 314 * the [onValue] callback will be called with that value. | 338 * the [onValue] callback will be called with that value. |
| 315 * If this future is already completed, the callback will not be called | 339 * If this future is already completed, the callback will not be called |
| 316 * immediately, but will be scheduled in a later microtask. | 340 * immediately, but will be scheduled in a later microtask. |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 615 * theFuture.catchError(thisCompleter.completeError); | 639 * theFuture.catchError(thisCompleter.completeError); |
| 616 * | 640 * |
| 617 */ | 641 */ |
| 618 void completeError(Object error, [StackTrace stackTrace]); | 642 void completeError(Object error, [StackTrace stackTrace]); |
| 619 | 643 |
| 620 /** | 644 /** |
| 621 * Whether the future has been completed. | 645 * Whether the future has been completed. |
| 622 */ | 646 */ |
| 623 bool get isCompleted; | 647 bool get isCompleted; |
| 624 } | 648 } |
| OLD | NEW |