traverseListWithIndex<A, B> static method

Task<List<B>> traverseListWithIndex<A, B>(
  1. List<A> list,
  2. Task<B> f(
    1. A a,
    2. int i
    )
)

Map each element in the list to a Task using the function f, and collect the result in an Task<List<B>>.

Each Task is executed in parallel. This strategy is faster than sequence, but the order of the request is not guaranteed.

If you need Task to be executed in sequence, use traverseListWithIndexSeq.

Same as Task.traverseList but passing index in the map function.

Implementation

static Task<List<B>> traverseListWithIndex<A, B>(
  List<A> list,
  Task<B> Function(A a, int i) f,
) =>
    Task<List<B>>(
      () => Future.wait<B>(
        list.mapWithIndex(
          (a, i) => f(a, i).run(),
        ),
      ),
    );