for (var i in arr) {
console.log(i);
$.get(searchURL, function (result) {
console.log(result);
}); // end of inner async request
} // end of outer arr
In this case, I am seeing
1
2
3
result
result
result
but what I intended and expected was
1
result
2
result
3
result
I assume this happens because each ajax call is taking longer than the entire for loop's execution. Is this right? If so, what is the conventional way of doing this?
update:
I found using jQuery's $.each
function instead of for loop
gives me the expected output. Is $.each
function intended to work with async calls inside loops?