I am implementing the IIFE method when wrapping a for a loop around an async/ajax call.
var j = 4;
for (var i = 0; i < j; i++) {
(function(cntr) {
asyncCall(function() {
console.log(cntr);
});
})(i);
}
The problem is that when I console.log cntr, I get all of the values, but they have a random order. Let's say I have a for loop from 0-4. It will print these values in a random order, like 2,1,3,4,0. This changes every time I rerun the code.
Edit: The question linked to most certainly is not the answer. Please pay more attention before marking as duplicate. I'm also not even using nodejs...
asyncCalldoes?asyncCallhas random delay - you should get random numbers like in your case. To make it work, I would use Chain Promisecntr? Looksundefinedto me. Oh, also you should change)(i)at the end of your self-executing function to(i)), as some Browsers are not accepting the other way now.