我无法在jQuery中使用always()触发我的Deferred。 done()和fail()工作正常,但不知怎的,我必须做错事,因为always()永远不会触发。
使用$。当它工作时......
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");
});
答案 0 :(得分:2)
回答我自己的问题:构造函数有三个参数,但最后一个是progressCallbacks。你需要单独调用always()。
var validate_date = Validator.validate_date(6).then(doneCallback, failCallback);
validate_date.always(alwaysCallback);
var validate_something = Validator.validate_something(1).then(doneCallback, failCallback);
validate_something.always(alwaysCallback);
var validationPromise = $.when(
validate_date,
validate_something
);