Example of when in Promises
by sandeep45
HTML
<body />
JavaScript
d1 = $.Deferred();
p1 = d1.promise();
d2 = $.Deferred();
p2 = d2.promise();
d3 = $.Deferred();
p3 = d3.promise();
p4 = $.when(p1,p2,p3);
p4.then(function(){ $("body").append("<h1>p4 which is combnation of p1, p2 and p3 is now done</h1>"); });
window.setTimeout(function(){
$("body").append("<h4>d1 is resolved as 1 second has paased");
d1.resolve();
}, 1000);
window.setTimeout(function(){
$("body").append("<h4>d2 is resolved as 2 second has paased");
d2.resolve();
}, 2000);
window.setTimeout(function(){
$("body").append("<h4>d3 is resolved as 3 second has paased");
d3.resolve();
}, 3000);