Skip to content

Instantly share code, notes, and snippets.

@hadaytullah
Last active October 25, 2019 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadaytullah/23100e8ca6fc963b7c6c33945f93f661 to your computer and use it in GitHub Desktop.
Save hadaytullah/23100e8ca6fc963b7c6c33945f93f661 to your computer and use it in GitHub Desktop.
Deferred/Promise Sample
function a(){
var d1=$.Deferred();
_.defer(function(){
console.log("a executed");
d1.resolve("resolved");
//d1.reject("reject");
},500);
d1.pipe(function successPipe(){
console.log("local pipe");
});
return d1.promise();
}
a().pipe(function(value){
console.log("a pipe: "+value);
return "pipe"
}).then(function(value){
console.log("a then1: "+value);
return "then1"
}).then(function(value){
console.log("a then2: "+value);
return "then2"
}).done(function(value){
console.log("a resolved: "+value);
return "done";
}).fail(function(value){
console.log("a failed: "+value);
});
var d1 = $.Deferred();
var d2 = $.Deferred();
var d3 = $.Deferred();
$.when( d1, d2, d3 ).done(function ( v1, v2, v3 ) {
console.log( v1 ); // v1 is undefined
console.log( v2 ); // v2 is "abc"
console.log( v3 ); // v3 is an array [ 1, 2, 3, 4, 5 ]
});
d1.resolve();
d2.resolve( "abc" );
d3.resolve( 1, 2, 3, 4, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment