forEachAsync<R> method

FutureOr<List<R>> forEachAsync<R>(
  1. FutureOr<R> processor(
    1. T e
    )
)

Perform a forEach allow FutureOr returns, then maps all the results in a single List.

Implementation

FutureOr<List<R>> forEachAsync<R>(FutureOr<R> Function(T e) processor) {
  var itr = iterator;

  if (!itr.moveNext()) {
    return [];
  }

  var e0 = itr.current;
  var r0 = processor(e0);

  if (r0 is Future<R>) {
    return _forEachAIterateAsync(processor, itr, r0, null);
  } else {
    var results = <R>[r0];

    while (itr.moveNext()) {
      var e1 = itr.current;
      var r1 = processor(e1);

      if (r1 is Future<R>) {
        return _forEachAIterateAsync(processor, itr, r1, results);
      } else {
        results.add(r1);
      }
    }

    return results;
  }
}