我无法让 always() 在 jQuery 中触发我的 Deferred。done() 和 fail() 工作正常,但不知何故我一定做错了什么,因为 always() 永远不会触发。
使用 $.when 它工作...
var Validator = {
validate_something: function(value) {
var deferred = new $.Deferred();
deferred.resolve();
return deferred.promise();
},
validate_date: function(value) {
var deferred = new $.Deferred();
deferred.resolve();
return deferred.promise();
}
};
function doneCallback() {
console.log("$.then.done() executed");
}
function failCallback() {
console.log("$.then.fail() executed");
}
function alwaysCallback() {
console.log("$.then.always() executed");
}
var validationPromise = $.when(
Validator.validate_date(6).then(doneCallback, failCallback, alwaysCallback),
Validator.validate_something(1).then(doneCallback, failCallback, alwaysCallback)
);
validationPromise.done(function() {
console.log("$.when.done() executed");
});
validationPromise.fail(function() {
console.log("$.when.fail() executed");
});
validationPromise.always(function() {
console.log("$.when.always() executed");
});