Skip to content

Instantly share code, notes, and snippets.

@hadaytullah
Last active Oct 25, 2019
Embed
What would you like to do?
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