Tutaj jest coś, co powinno wystarczyć:
$.whenAllDone = function() {
var deferreds = [];
var result = $.Deferred();
$.each(arguments, function(i, current) {
var currentDeferred = $.Deferred();
current.then(function() {
currentDeferred.resolve(false, arguments);
}, function() {
currentDeferred.resolve(true, arguments);
});
deferreds.push(currentDeferred);
});
$.when.apply($, deferreds).then(function() {
var failures = [];
var successes = [];
$.each(arguments, function(i, args) {
// If we resolved with `true` as the first parameter
// we have a failure, a success otherwise
var target = args[0] ? failures : successes;
var data = args[1];
// Push either all arguments or the only one
target.push(data.length === 1 ? data[0] : args);
});
if(failures.length) {
return result.reject.apply(result, failures);
}
return result.resolve.apply(result, successes);
});
return result;
}
Wyjazd this Fiddle, aby zobaczyć jak to działa.
Zasadniczo czeka na wszystkich Odroczonych, bez względu na to, czy zawiodą, czy nie, i zbiera wszystkie wyniki. Jeśli mamy awarie, zwrócony Odroczony zakończy się niepowodzeniem z listą wszystkich niepowodzeń i rozwiązany z wszystkimi sukcesami w inny sposób.
Możesz przyjrzeć się obietnicom http://api.jquery.com/promise/ – BlueBird
@BlueBird: Jak? obiecuję wymagany obiekt jquery, którego nie mam. Czy możesz dodać przykład? – Naor
@ BlueBird: '$ .when' już zwraca obiekt obietnicy, tj. OP już używa obietnic. –