That's because the promises resolve with the actual value in a $.ajax
call and not with the XHR itself.
If you only want the successes, you can resolve with the xhr itself instead of the result:
var requests = $.map(credentials, function(k, v){
var xhr = $.ajax({
url : '/path/to/my/uri',
type : 'post',
contentType : 'application/json',
data : JSON.stringify(v)
});
var xhr2 = xhr.then(function(result){ return xhr; }); //resolve with original
return xhr2;
});
Alternatively, you can handle errors yourself and access only the successful deferred objects, stealing from my own answer from here:
//returns only fulfilled values from a list of promises
function some(promises){
var d = $.Deferred(), results = [];
var remaining = promises.length;
for(var i = 0; i < promises.length; i++){
promises[i].then(function(res){
results.push(res); // on success, add to results
}).always(function(res){
remaining--; // always mark as finished
if(!remaining) d.resolve(results);
})
}
return d.promise(); // return a promise on the remaining values
}
And do:
function loginUser(credentials){
...
return some(requests);
}
loginUser(userData).then(function(results){
//results is only the results of successful requests here.
});