I am trying to make multiple AJAX calls in a loop using the code below.
for (var i = 0; i < 5; i++) {
console.log(i);
ajax_DatabaseAccessor.query("CheckInt", i, loadQuery);
function loadQuery(data) {
alert(DWRUtil.toDescriptiveString(data, 2));
}
}
After debugging the code, I noticed that the log was written five times before the AJAX calls were executed, and the parameter being passed is always 4. This discrepancy makes me believe that this is due to the asynchronous nature of the call. How can I modify the code to make it synchronous? I am aware that jQuery has an option to set async to false, but I want to achieve this in plain JavaScript without relying on jQuery. Thank you.