mapAsync<V, R> function

Future<List<R>> mapAsync<V, R>(
  1. Iterable<V> source,
  2. Future<R> toElement(
    1. V element
    ), [
  3. int parallels = 1
])

The current elements of source iterable modified by toElement. Runs in parallels and return a Future<List>.

Example:

  final files = [
    'file1',
    'file2',
    'file3',
    'file4',
    'file5',
    'file6',
    'file7',
  ];
  final result = await mapAsync<String, bool>(files, (file) => upload(file), 3);
  print(result);

Implementation

Future<List<R>> mapAsync<V, R>(
    Iterable<V> source, Future<R> Function(V element) toElement,
    [int parallels = 1]) async {
  final List<R> result = <R>[];
  for (final Iterable<V> group in groupBySize(source, parallels)) {
    final List<R> res = await Future.wait(group.map(toElement));
    result.addAll(res);
  }
  return result;
}