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...