I have this piece of code in JS as described below
for (id in ids) {
if (id needs to be called) {
console.log(id); // statement 1
$http.post('someEndpoint', {id : id}).then(function success(response) {
console.log(id); // statement 2
someDataStructure[id].data = response.data;
});
}
}
Assuming ids = [1, 2, 3]
, at statement 1, 1 2 3
is printed on the console. At statement 2, 3 3 3
is printed. This is obviously less than ideal and I am losing data for 1 2
. Also, I must add that the endpoint is getting requests for 1 2 3
, so at this point, I am really confused. It would be great if someone could throw some light and help me understand how to fix this.