Created
August 22, 2012 15:37
-
-
Save crazy4groovy/3426872 to your computer and use it in GitHub Desktop.
Promise pattern, after reading http://www.dzone.com/articles/winjs-unpacking-promises
This file contains hidden or 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
| function getRandomNumberHigherThanAsync(minimum) { | |
| var someNumber = (Math.random()*20) + minimum; | |
| return { | |
| 'then' : function(cb){ return cb(someNumber); }, | |
| 'val' : someNumber | |
| }; | |
| } | |
| function alertMe(num) { | |
| alert(num); | |
| return { | |
| 'then' : function(cb){ return cb(num); }, | |
| 'val' : num | |
| }; | |
| } | |
| var a = getRandomNumberHigherThanAsync; | |
| var b = alertMe; | |
| var final1 = a(10).then(b).then(a).then(b).then(a); | |
| alert("final: " + final1.val); | |
| var final2 = a(10).then(b).then(a).then(b).then(a) | |
| .then(function(num) { | |
| var smallNum = num / 2; | |
| alert(smallNum); | |
| return { | |
| 'then' : function(cb){ return cb(smallNum); }, | |
| 'val' : smallNum | |
| }; | |
| }); | |
| alert("final: " + final2.then(a).val); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment