0

I am having trouble getting always() to fire on my Deferred in jQuery. done() and fail() work fine, but somehow I must be doing something wrong, because always() never fires.

With $.when it works...

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");
});
​

See http://jsfiddle.net/6j6K2/

Asciiom
  • 9,867
  • 7
  • 38
  • 57
Joseph Tura
  • 6,290
  • 8
  • 47
  • 73

1 Answers1

2

Answering my own question: the constructor takes three arguments, but the last one is for progressCallbacks. You need to call always() separately.

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
);
Joseph Tura
  • 6,290
  • 8
  • 47
  • 73
  • 1
    You'd better use `done`, `fail` and `progress` all the time. `then` is an alias to `pipe` as of `1.8.0` which means it's much much slower since it has to account for potentially convoluted scenarios. – Julian Aubourg Sep 19 '12 at 13:01
  • 1
    Thanks for the tip. I have actually started doing that anyhow, because it results in much more readable code. – Joseph Tura Sep 19 '12 at 13:14