JS example 1
this code assumes you want to abort the other requests if any one request fails (via a 404 or 500 response, or times out) , and does not need to evaluate the data response to determine a business logic failure scenario. $.when()
The method will resolve its master Deferred as soon as all the
Deferreds resolve, or reject the master Deferred as soon as one of the
Deferreds is rejected.
$.when(fireRequest(1), fireRequest(2),fireRequest(3))
.then(myAllSuccessfulFunc, oneFailedFunc);
function myAllSuccesfulFunc(req1,req2,req3){
//everything returned a 200.
alert("these are not the droids you are looking for");
};
function oneFailedFunc(req1,req2,req3){
//* each req looks like [ "not success", statusText, jqXHR ] */
//feel free to check what failed, but I don't know what you need
req1[2].abort();
req2[2].abort();
req3[2].abort();
};
js example 2
you actually need to parse a successful request for data in the response to see if you should fail the other requests due to logic from the back end.
var stop = 4;
//if you are sure fireRequest(x) returns a good promise object, do this:
callNext(fireRequest(1),1);
function callNext(promise, currentIndex){
promise.done(function(ajaxArgs){
var jqXHR = ajaxArgs[2];
//replace this with some logic check that makes sense to your app
if(/error/.test(jqXHR.responseText)){
//do something
}else if(currentIndex <stop){
callNext(fireRequest(currentIndex+1),currentIndex+1);
}).fail(function(ajaxArgs){
//server returned a 404, or a 500, or something not a success.
});
};