forEach method

Future forEach (
  1. void action(
    1. T element

Executes action on each element of this stream.

Completes the returned Future when all elements of this stream have been processed.

If this stream emits an error, or if the call to action throws, the returned future completes with that error, and processing stops.


Future forEach(void action(T element)) {
  _Future future = new _Future();
  StreamSubscription subscription;
  subscription = this.listen(
      (T element) {
        // TODO(floitsch): the type should be 'void' and inferred.
        _runUserCode<dynamic>(() => action(element), (_) {},
            _cancelAndErrorClosure(subscription, future));
      onError: future._completeError,
      onDone: () {
      cancelOnError: true);
  return future;