I am learning how to use jQuery's deferred, so I made a simple little example, so I could mess around.
function a() {
var d = new $.Deferred,
$A = $('#A'),
$P = $('#P').progressbar();
setTimeout(function() {
$A.css('background-color', 'blue');
d.notifyWith($P, [.5]);
}, 2000);
setTimeout(function() {
$A.text('test');
d.notifyWith($P, [1]);
d.resolveWith($P, ['done']);
}, 4000);
return d.promise();
}
$('#G').click(function() {
a().progress(function(x) {
this.progressbar({
value: x * 100
});
}).done(function(x) {
alert(x)
});
});
DEMO: http://jsfiddle.net/NTICompass/3DDSa/3/
This example works great. After the operations complete, an alert pops up.
I read then you can combine multiple promises with $.when
(which itself returns a promise), so I decided to break a()
into 2 functions:
function a() {
var d = new $.Deferred,
$A = $('#A');
setTimeout(function() {
$A.css('background-color', 'blue');
d.notify(.5);
}, 2000);
return d.promise();
}
function b() {
var d = new $.Deferred,
$A = $('#A');
setTimeout(function() {
$A.text('test');
d.notify(1);
d.resolve('done');
}, 4000);
return d.promise();
}
$('#G').click(function() {
var $P = $('#P').progressbar();
$.when(a(), b()).progress(function(x) {
$P.progressbar({
value: x * 100
});
}).done(function(x) {
alert(x)
});
});
DEMO: http://jsfiddle.net/NTICompass/3DDSa/8/
I use $.when(a(), b())
to combine the 2 promises, but it's not working. The progress bar goes to 50%, but not to 100%, and my .done
is never called.
It seems the the .notify
(and the .resolve
) inside b()
aren't having any effect. What is wrong here? How do I use $.when
to combine 2 promises?