I currently have a series of ajax calls that need to be performed in a certain order. None of them are exactly doable in parallel.
But in essence all performSeriesofCalls
does is make ajax calls in the exact order of Call1 -> Call2 -> Call3 -> Call4 -> Call5
.
performSeriesofCalls: function () {
doThisFirst(function () {
Service.call1(arg1,arg2).done(function (modelData) {
updateModel(modelData);
var saveId = modelData.Id1;
var discardId = modelData.id2;
Service.call2(saveId, arg4).done(function () {
Service.call3(discardId).done(function () {
Service.call4(saveId).done(function () {
Service.call5(saveId).done(function (savedModel) {
updateModel(savedModel);
updateMessage("Performed");
});
});
});
});
});
});
}
Looked at examples here and Stackoverflow answer: ordering ajax calls and tried refactoring into the following using jQuery.when(), but I want to know if can do better.
performSeriesofCalls: function () {
doThisFirst(function () {
Service.call1(arg1,arg2).done(function (modelData) {
updateModel(modelData);
var saveId = modelData.Id1;
var discardId = modelData.id2;;
jQuery.when(
Service.call2(saveId, arg4)
).done(function () {
jQuery.when(
Service.call3(discardId),
Service.call4(saveId)
).done(function () {
Service.call5(saveId).done(function (savedModel) {
updateModel(savedModel);
updateMessage("Performed");
});
});
});
});
});
}
note: Service.js contains the actual ajax requests for the sake of modularity.
sample calls in Service.js
/**
* Update Status
* @returns {jqXHR} From the jQuery.ajax() call
*/
call2: function (quoteId, action) {
return jQuery.ajax({
method: "POST",
url: serviceURL + "/endpoint/" + quoteId + "/action",
data: JSON.stringify(action),
contentType: "application/json"
});
}