我在这里错过了什么:
var Services = {
TestService: {
init: function() {
var promises = [];
for (var i = 0; i < 3; i++) {
var def = $.Deferred();
Services.TestService.test(i).then(function(a) {
def.resolve();
}, function() {
console.log("test rejected");
def.reject();
});
promises.push(def.promise());
}
$.when.apply($, promises).then(function() {
console.log("Finished all promises");
}, function() {
console.log("eror");
});
},
test: function(i) {
var def = $.Deferred();
setTimeout(function() {
console.log("test timeout expired: " + i.toString());
def.resolve("ok: " + i.toString());
}, 1000);
return def.promise();
}
}
}
Services.TestService.init();
它只打印:
test timeout expired: 0
test timeout expired: 1
test timeout expired: 2
为什么没有when
完成或失败(当时实施)启动。我错过了什么细节?使用jQuery 1.9.1。 (<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
)