Hey there! Currently, I am working on creating a batch update for my local store using a for loop and asynchronous Ajax calls.
However, I'm facing an issue where the loop continues running even before the Ajax call has successfully completed. Is there a way to make the for loop wait until the response of the Ajax call is received before moving on?
I would greatly appreciate any assistance. Thank you in advance!
Here is a snippet of my code:
var counter = 0;
var totalRow = 3000;
for (var i = 0; counter <= totalRow; i++) {
var defectssurl = 'https://test.com/mywcf.svc/GetListAllPaging?id=' + counter;
Ext.Ajax.request({
url: defectssurl,
method: "POST",
params: '',
success: function (resp) {
console.log("Loading first 500 records");
var data = Ext.JSON.decode(resp.responseText);
if (counter == 0) {
defectsLocalStore.getProxy().clear();
defectsLocalStore.removeAll();
}
Ext.Array.each(data, function (record) {
counter++;
defectsLocalStore.add(record);
});
defectsLocalStore.sync();
if (counter >= totalRow) {
pCallback();
}
},
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
failure: function (resp) {
}
});
}