Created
March 18, 2013 14:15
-
-
Save ukyo/5187430 to your computer and use it in GitHub Desktop.
再帰 with promise pattern ref: http://qiita.com/items/86ad078cd6d9e1bf0a3d
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 fact(n) { | |
| var d = $.Deferred(); | |
| setTimeout(function() { | |
| function onResolved(result) { | |
| d.resolve(result * n); | |
| } | |
| n > 1 ? fact(n - 1).then(onResolved) : d.resolve(n); | |
| }, 100); | |
| return d.promise(); | |
| } | |
| fact(5).then(console.log.bind(console)); // 120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment