I make one or more REST / ajax calls to validate some user data. The remaining calls work well and the information is returned. The problem I am facing is not the part of the code that looks something like this.
function ensureUsers(recipients){
var promises = [];
for(var key in recipients){
var payload = {'property':recipients[key]};
promises.push( $.ajax({...}));
}
return $.when.apply($,promises);
}
....
ensureUsers(users)
.done(function(){
console.log(arguments);
)}
If there is more than one user in the source array, the arguments in my code are .donestructured as follows:
[[Object,"success",Object],[Object,"success",Object]...]
Then I can iterate over each result, check the status and continue.
However, if there is only one user in the source array, it .donereceives the following arguments:
[Object,"success",Object]
It seems strange to me that the structure of the returnee will change so. I could not find anything about this particular problem, so I hacked a solution
var promises = Array.prototype.slice.call(arguments);
if(!Array.isArray(promises[0])){
promises = [promises];
}
, ? promises 1 ajax jQuery?