Index: packages/quiver/lib/src/async/iteration.dart |
diff --git a/packages/quiver/lib/src/async/iteration.dart b/packages/quiver/lib/src/async/iteration.dart |
index 4132573ac9bfa9583f8eee2bdcdf1186072d82f4..d68affa149357246756634f42e57a35e70fd8665 100644 |
--- a/packages/quiver/lib/src/async/iteration.dart |
+++ b/packages/quiver/lib/src/async/iteration.dart |
@@ -14,56 +14,49 @@ |
part of quiver.async; |
-/** |
- * An asynchronous callback that returns a value. |
- */ |
+/// An asynchronous callback that returns a value. |
typedef Future<T> AsyncAction<T>(e); |
-/** |
- * An asynchronous funcuntion that combines an element [e] with a previous value |
- * [previous], for use with [reduceAsync]. |
- */ |
+/// An asynchronous funcuntion that combines an element [e] with a previous |
+/// value [previous], for use with [reduceAsync]. |
typedef Future<T> AsyncCombiner<T>(T previous, e); |
-/** |
- * Calls [action] for each item in [iterable] in turn, waiting for the Future |
- * returned by action to complete. |
- * |
- * If the Future completes to [true], iteration continues. |
- * |
- * The Future returned completes to [true] if the entire iterable was processed, |
- * otherwise [false]. |
- */ |
+/// Calls [action] for each item in [iterable] in turn, waiting for the Future |
+/// returned by action to complete. |
+/// |
+/// If the Future completes to [true], iteration continues. |
+/// |
+/// The Future returned completes to [true] if the entire iterable was |
+/// processed, otherwise [false]. |
Future doWhileAsync(Iterable iterable, AsyncAction<bool> action) => |
_doWhileAsync(iterable.iterator, action); |
-Future _doWhileAsync( |
- Iterator iterator, AsyncAction<bool> action) => (iterator.moveNext()) |
- ? action(iterator.current).then((bool result) => |
- (result) ? _doWhileAsync(iterator, action) : new Future.value(false)) |
- : new Future.value(true); |
+Future _doWhileAsync(Iterator iterator, AsyncAction<bool> action) async { |
+ if (iterator.moveNext()) { |
+ return await action(iterator.current) |
+ ? _doWhileAsync(iterator, action) |
+ : false; |
+ } |
+ return true; |
+} |
-/** |
- * Reduces a collection to a single value by iteratively combining elements |
- * of the collection using the provided [combine] function. Similar to |
- * [Iterable.reduce], except that [combine] is an async function that returns a |
- * [Future]. |
- */ |
+/// Reduces a collection to a single value by iteratively combining elements of |
+/// the collection using the provided [combine] function. Similar to |
+/// [Iterable.reduce], except that [combine] is an async function that returns |
+/// a [Future]. |
Future reduceAsync(Iterable iterable, initialValue, AsyncCombiner combine) => |
_reduceAsync(iterable.iterator, initialValue, combine); |
-Future _reduceAsync(Iterator iterator, currentValue, AsyncCombiner combine) { |
+Future _reduceAsync(Iterator iterator, current, AsyncCombiner combine) async { |
if (iterator.moveNext()) { |
- return combine(currentValue, iterator.current) |
- .then((result) => _reduceAsync(iterator, result, combine)); |
+ var result = await combine(current, iterator.current); |
+ return _reduceAsync(iterator, result, combine); |
} |
- return new Future.value(currentValue); |
+ return current; |
} |
-/** |
- * Schedules calls to [action] for each element in [iterable]. No more than |
- * [maxTasks] calls to [action] will be pending at once. |
- */ |
+/// Schedules calls to [action] for each element in [iterable]. No more than |
+/// [maxTasks] calls to [action] will be pending at once. |
Future forEachAsync(Iterable iterable, AsyncAction action, {int maxTasks: 1}) { |
if (maxTasks == null || maxTasks < 1) { |
throw new ArgumentError("maxTasks must be greater than 0, was: $maxTasks"); |
@@ -92,16 +85,17 @@ Future forEachAsync(Iterable iterable, AsyncAction action, {int maxTasks: 1}) { |
if (!scheduleTask() && pending == 0) { |
completer.complete(); |
} |
- }).catchError((e) { |
+ }).catchError((e, stack) { |
if (failed) return; |
failed = true; |
- completer.completeError(e); |
+ completer.completeError(e, stack); |
}); |
}); |
return true; |
} |
return false; |
} |
+ |
while (scheduleTask()) {} |
return completer.future; |
} |