I'm using $.when().then(success, fail).always() model to send 3 asynchronous ajax calls.
here's my code
$.when(
$.ajax({
url: NewsCategoryUrl,
beforeSend: function () {
//alert('NewsCategoryUrl berfore send by Boda');
},
headers: {
'accept': 'application/json;odata=verbose',
'content-type': 'application/json;odata=verbose'
},
success: function (data) {
//alert('NewsCategoryUrl done');
$.each(data.d.results, function (index, item) {
NewsCategoryList.push(G_G.GetNewsCategoryObject(item));
//alert(JSON.stringify(NewsCategoryList[index]));
});
// alert('after loop: ' + NewsCategoryList);
},
error: function (jqXHR, errorThrown) {
alert(errorThrown.stack);
}
}),
$.ajax({
url: NewsFeedUrl,
beforeSend: function () {
alert('NewsFeedUrl berfore send by Boda');
},
headers: {
'accept': 'application/json;odata=verbose',
'content-type': 'application/json;odata=verbose'
},
success: function (data) {
//alert('NewsFeedUrl done');
$.each(data.d.results, function (index, item) {
NewsFeedList.push(G_G.GetNewsFeedObject(item));
//alert(JSON.stringify(NewsFeedList[index]));
});
//alert('after loop: ' + NewsFeedList);
},
error: function (jqXHR, errorThrown) {
alert(errorThrown.stack);
}
}),
$.ajax({
url: UpdatesUrl,
beforeSend: function () {
alert('UpdatesUrl berfore send by Boda');
},
headers: {
'accept': 'application/json;odata=verbose',
'content-type': 'application/json;odata=verbose'
},
success: function (data) {
//alert('UpdatesUrl done');
$.each(data.d.results, function (index, item) {
updates.push(G_G.GetUpdateObject(item));
//alert(JSON.stringify(updates[index]));
});
//alert('after loop: ' + updates);
},
error: function (jqXHR, errorThrown) {
alert(errorThrown.stack);
}
})).then(function () {
/* alert("got 'ya all :D");
alert(NewsCategoryList);
alert(NewsFeedList);
alert(updates);*/
SaveUpdatesToLocalstorage(updates);
NewsCategoryReandering();
SliderRendering(3);
}, function () {
alert('some went wrong');
}).always(function () {
alert('in always');
myApp.closeModal('.popup-splash');
});
the problem is that when I try to comment out any of the beforeSend in the 2nd or 3rd ajax calls, I end up in the fail section with alert ("some went wrong") and undefined results..
All I could find after googling it, that It's about timing issue (mostly about synchronous requests unlike mine), and one SO answer about ASI which I couldn't verify over my case..
note that when I commented the first alert in the first ajax request everything continue to work normally..
Any help?