Created
July 5, 2011 12:12
-
-
Save francescoagati/1064727 to your computer and use it in GitHub Desktop.
jQuery:deferred an example of promise chained deferred with pipe and done. Coffeescript and javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
promise= (fn) -> df= $.Deferred(fn).promise() | |
df=promise (df) -> setTimeout( (-> df.resolve(1000)), 1 ) | |
df.pipe (x) -> | |
promise (df) -> setTimeout( (-> df.resolve(x+1000)), 2 ) | |
.pipe (x) -> | |
promise (df) -> setTimeout( (-> df.resolve(x+1000)), 2 ) | |
.pipe (x) -> | |
promise (df) -> setTimeout( (-> df.resolve(x+1000)), 2 ) | |
.pipe (x) -> | |
console.log x | |
.done (x) -> console.log "ufo" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var df, promise; | |
promise = function(fn) { | |
var df; | |
return df = $.Deferred(fn).promise(); | |
}; | |
df = promise(function(df) { | |
return setTimeout((function() { | |
return df.resolve(1000); | |
}), 1); | |
}); | |
df.pipe(function(x) { | |
return promise(function(df) { | |
return setTimeout((function() { | |
return df.resolve(x + 1000); | |
}), 2); | |
}); | |
}).pipe(function(x) { | |
return promise(function(df) { | |
return setTimeout((function() { | |
return df.resolve(x + 1000); | |
}), 2); | |
}); | |
}).pipe(function(x) { | |
return promise(function(df) { | |
return setTimeout((function() { | |
return df.resolve(x + 1000); | |
}), 2); | |
}); | |
}).pipe(function(x) { | |
return console.log(x); | |
}).done(function(x) { | |
return console.log("ufo"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment