Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Created December 6, 2011 09:35
Show Gist options
  • Save dhavaln/1437552 to your computer and use it in GitHub Desktop.
Save dhavaln/1437552 to your computer and use it in GitHub Desktop.
jQuery Differed Example
function showData() {
console.log("final value :" +newArray.length);
console.log(newArray);
}
var newArray = [];
function method1() {
return $.Deferred(function( dfd ){
self.setTimeout(function(){
newArray.push("hello");
dfd.resolve();
}, 3000);
}).promise();
}
function method2(){
return $.Deferred(function( dfd ){
self.setTimeout(function(){
newArray.push("world");
dfd.resolve();
}, 3000);
}).promise();
}
console.log("starting method1");
$.when(method1()).then(
function(){
console.log("starting method2");
$.when(method2()).then(showData);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment