dispatch<T, R> method

Future<List<R>> dispatch<T, R>(
  1. Iterable<T> items,
  2. Future<R> worker(
    1. T item
    ), {
  3. bool shouldStop(
    1. R
    )?,
})

Dispatches items to be processed by worker according to this concurrency strategy.

This eliminates higher-order closure boilerplate (e.g. items.map((i) => () => asyncOp(i))) and supports clean function tear-offs.

Results are collected and returned in the exact original order of the input items.

Example:

final users = await Concurrency.bounded(3).dispatch(
  userIds,
  (id) => api.fetchUser(id),
);

// Or with direct function tear-off:
final users = await Concurrency.bounded(3).dispatch(userIds, api.fetchUser);

Implementation

Future<List<R>> dispatch<T, R>(
  Iterable<T> items,
  Future<R> Function(T item) worker, {
  bool Function(R)? shouldStop,
}) => process(
  items.map(
    (item) =>
        () => worker(item),
  ),
  shouldStop: shouldStop,
);