Update: this issue was a result of jQuery 1.7 vs 1.8. Do not ever use promises in 1.7 beacuse they aren't chainable with returning a promise inside a .then
. 1.8 looks like they didn't mess it up.
http://jsfiddle.net/delvarworld/28TDM/
// make a promise
var deferred = $.Deferred();
promise = deferred.promise();
// return a promise, that after 1 second, is rejected
promise.then(function(){
var t = $.Deferred();
setTimeout(function() {
console.log('rejecting...');
t.reject();
}, 1000);
return t.promise();
});
// if that promise is successful, do this
promise.then(function() {
console.log('i should never be called');
})
// if it errors, do this
promise.fail(function() {
console.log('i should be called');
});
deferred.resolve();
Expected: 'i should be called'
Actual: 'i should never be called'
Problem: I want to chain callbacks and have any one of them be able to break the chain and trigger the fail
function, and skip the other chained callbacks. I don't understand why all of the thens are triggered and the fail is not triggered.
I'm coming from NodeJS's Q library, so I tried it with .then
first. However, changing it to .pipe
has no effect.
t
from.then()
doesn't do anything. Rejectingt
also doesn't affect the original deferred object in any way. Callbacks from.then
aren't called until after the deferred object they are applied to are either resolved or rejected. Once they are resolved or rejected, they can't be rejected or resolved again. – Alfalfa.then
with.done
since in your case they do exactly the same thing. – Alfalfathen
adds it to the chain. what's the correct way to chain otherwise? – Zaccaria