If I create a single Deferred object, then it reports progress like I would expect.
var d1 = function() {
var d = $.Deferred();
d.notify('d1');
d.resolve('d1');
return d.promise();
};
d1().progress(function(a) {
log('Progress: ' + a);
});
However, if I chain two Deferred objects using then(), the progress callbacks are not called.
d1().then(d1).progress(function(a) {
log('Progress: ' + a);
});
It seems to me that then() should propagate the progress notification, but it doesn’t appear to be the case. Am I missing something?
I have tested this with jQuery 1.8.0 and 1.8.1. A full working example is here: http://jsfiddle.net/eWQuG/13/
I can see what’s going on but it’s slightly tricky to explain. You may have to read this through a couple of times …
In Test 1, the promise passed to
test()is the “original promise” ie. the object returned byd1(). Simple.In Test 2, the object passed to
test()is a new promise resulting fromd1().then(d2).then(d3). Less simple. This new promise will have inherited only some of the original promise’s properties – not including its progressCallbacks (as put in place in the original promise withnotify()). This is reasonable for a promise returned by.then(), because.then()is designed to put in place its own doneCallbacks, failCallbacks and progressCallbacks, not to propagate those of the deferred/promise to its immediate left. In short,.then()does exactly what its name implies – it specifies what is to be done after its preceding deferred/promise has been processed.You can better see what’s going on by keeping a reference to the original promise (ie. the output of
d1()), thus allowing the simple Test 1 to be run both before and after Test 2.Here is a composite Test 3 :
DEMO
In the log, you will see that the behaviour of
test(p)is identical in both cases. However,test(p.then(d2).then(d3))behaves differently because the object passed is no longer the original promise but a new promise with no progressCallbacks.EDIT
Ryan, something maybe worth considering in achieving what you want with :
is that a the chain can be rearranged as follows :
The progress callback is thus applied to the original promise and because
.progress()propagates the original promise down the chain,.then()will still act as before.Both versions are valid but would be applicable in different circumstances.
If I’m right then maybe this is the short answer to your question.