I have a method in my TypeScript file that looks like this:
getInitialBatches() {
var i = 0;
for (var dto of this.transferDTO.stockMovesDTOs) {
i++;
this.queryResourceService
.getBatchIdUsingGET(this.batchParams)
.subscribe((data) => {
this.allBatches[i] = data;
});
}
}
Due to getBacthIdUsingGET
being a callback function, it is not working as intended. The loop control variable i
gets incremented by more than 1 by the time the callback is invoked, resulting in values being placed into random indexes of the allBatches
array. How can I resolve this issue?