First of all you are making an ajax request on each for loop which seems a bit wasting your server resources.
If that function will be getting called a lot then i would suggest you to refactor your code
to make as less queries as possible.
Anyways, here's how i'd do it
function countHealthy(urls) {
var healthy = 0;
var reqs = [];
for (var i = 0; i < urls.length; ++i) {
reqs.push(
$.ajax({
url: urls[i],
id: i
})
);
}
}
$.when.apply($, reqs).then(function() {
// On success
++healthy;
}).fail(function() {
// When failed
}).always(function() {
// this will be run no matter the outcome
});
Haven't had the chance to try it out, so i'm not sure if i've made a syntax error etc.
Changes to your piece of code :
Removed async = true
, ignore this if you had set it to be false somewhere in your code before.
You can get an idea of how "when.apply" works,
source