Future forEach(Iterable input, dynamic f(element))

Perform an async 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 return values of all Futures are discarded. Any errors will cause the iteration to stop and will be piped through the returned Future.

If f returns a non-Future, iteration continues immediately. Otherwise it waits for the returned Future to complete.


static Future forEach(Iterable input, f(element)) {
  Iterator iterator = input.iterator;
  return doWhile(() {
    if (!iterator.moveNext()) return false;
    return new Future.sync(() => f(iterator.current)).then((_) => true);