mapBatched<A, T> function
Batch async (e.g. 5 at a time). Roadmap #182.
Implementation
Future<List<T>> mapBatched<A, T>(
Iterable<A> items,
AsyncMapper<A, T> fn, {
int batchSize = 5,
}) async {
final List<A> list = items.toList();
final List<T> out = <T>[];
for (int i = 0; i < list.length; i += batchSize) {
final batch = list.skip(i).take(batchSize).map(fn);
final List<T> results = await Future.wait(batch, eagerError: false);
// ignore: prefer_spread_over_addall -- accumulates across loop iterations; rebuilding via spread each pass would be O(n^2)
out.addAll(results);
}
return out;
}