Async functions defined in loops are called after the loop is finished.
So if they use a value that changes on each iteration (such as
index
in var index = 0; index < 2; index++
), those
functions will be called with the last value of index
(in this
case 2
).
Wrap the async part in a function and pass index
to that as a
parameter.