Created
August 10, 2012 10:10
-
-
Save BorisKozo/3313164 to your computer and use it in GitHub Desktop.
Calculating Fibonacci asynchronously
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
var data = [0,1]; | |
function retrieveData(callback){ | |
var x = data[data.length - 1]; | |
var y = data[data.length - 2]; | |
setTimeout(function(){ | |
callback(x,y); | |
},0); | |
} | |
function calculateSum(x,y,callback){ | |
var sum = x+y; | |
setTimeout(function(){ | |
callback(sum); | |
},0); | |
} | |
function addSum(sum,callback){ | |
data.push(sum); | |
setTimeout(callback,0); | |
} | |
//Adds count fibonnacci elementd to data | |
function fibonacci(count){ | |
var i; | |
for (i=0; i<count; i++){ | |
retrieveData(function(x,y){ | |
calculateSum(x,y,function(sum){ | |
addSum(sum,function(){ | |
console.log(data); | |
}); | |
}); | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment