Skip to content

Instantly share code, notes, and snippets.

@ukyo
Created March 18, 2013 14:15
Show Gist options
  • Select an option

  • Save ukyo/5187430 to your computer and use it in GitHub Desktop.

Select an option

Save ukyo/5187430 to your computer and use it in GitHub Desktop.
再帰 with promise pattern ref: http://qiita.com/items/86ad078cd6d9e1bf0a3d
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