Skip to content
Browse files

Merge pull request #2 from jtwebman/master

This change should allow it to scale better though it will calculate much slower.
  • Loading branch information...
2 parents 745bf9f + 8cd616f commit 3567d21e8b1f3e0ea11b7d2f374b56b1ed5333ec @NuckChorris committed
Showing with 19 additions and 11 deletions.
  1. +19 −11 index.js
View
30 index.js
@@ -1,13 +1,21 @@
module.exports = function (num) {
- function fibonacci (n) {
- if (n < 2) {
- return 1;
- } else {
- return fibonacci(n-2) + fibonacci(n-1);
- }
+ function fibonacci(n, done) {
+ if (n < 2) {
+ done(1);
+ } else {
+ process.nextTick(function() {
+ fibonacci(n - 2, function(val1) {
+ fibonacci(n - 1, function(val2) {
+ done(val1 + val2);
+ });
+ });
+ });
+ }
+ }
+ return function (req, res, next) {
+ fibonacci(num, function(value) {
+ req.fibonacci = value;
+ next();
+ });
}
- return function (req, res, next) {
- req.fibonacci = fibonacci(num);
- next();
- }
-}
+}

0 comments on commit 3567d21

Please sign in to comment.
Something went wrong with that request. Please try again.