This is what i am trying to achieve,
I have a sorted array which i pass to jQuery each. Inside each there is an ajax call which will fetch me the desired data and every time push into another array(lets call it allJsonData). Finally i display allJsonData. Problem is whenever i display allJsonData, elements are always displayed inconsistently (not alphabetically/random order). I am expecting allJsonData to be displayed alphabetically (that is AList data first, BList data second, CList data third and so on). I am new to jQuery deferred and promise. Thanks in advance.
var sortedArray = [AList, BList, CList, DList];
var promises = [];
var allJsonData = [];
$.each(sortedArray, function (index, value) {
var dfd = $.Deferred();
var url = _spPageContextInfo.webAbsoluteUrl + ('/_api/Web/Lists/GetByTitle(' + "'" + value + "'" + ')/Items? + "SomeFilterParameters";
//AJAX CALL HERE//
.done(
function (approvedListItems) {
if (approvedListItems.d.results.length != 0) {
$.each(approvedListItems.d.results, function (i, col) {
allJsonData.push(col);//Push into master array
});
}//If closed
dfd.resolve(allJsonData);
}
);//Done closed
promises.push(dfd);
});//jQuery Each closed
return $.when.apply($, promises).promise();
/****AJAX CALL****/
getListItems: function(url) {
var dfd = $.Deferred();
$.ajax({
url: url,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
dfd.resolve(data);
},
error: function (error) {
dfd.reject(sender, args, "Error retrieving items");
}
});
return dfd.promise();
},