I have 2 different ajax post methods that both run on a loop (with each one finishing before the next one starts), before I was using async: false and everything worked fine, but it is now depreciated and throws errors. Is there an alternative method so that I can run the methods synchronously while still in my loop? I have tried wrapping each in their own function and calling using .done() but this keeps my post in the loop.
function postLoop() {
len = 3
backorderPayLoad = []
cancelPayLoad = []
for (var i = 0; i < len; i++) {
backorder = {
"quantity": 20,
"note": "Backorder",
}
cancel = {
"quantity": 20,
"note": "Cancel",
}
backorderPayLoad.push(backorder);
cancelPayLoad.push(cancel);
$.ajax({
url: myurl,
method: 'POST',
contentType: : "application/json",
async: false,
data: JSON.stringify(backorderPayLoad[i]),
beforeSend: function(xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
})
$.ajax({
url: myurl2,
method: 'POST',
contentType: : "application/json",
async: false,
data: JSON.stringify(cancelPayLoad[i]),
beforeSend: function(xhr, settings)
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
})
}
}