Skip to content

Instantly share code, notes, and snippets.

@codyromano
Created February 19, 2018 00:20
Show Gist options
  • Select an option

  • Save codyromano/fc65690878ee6ceaf42a85f70715c00c to your computer and use it in GitHub Desktop.

Select an option

Save codyromano/fc65690878ee6ceaf42a85f70715c00c to your computer and use it in GitHub Desktop.
function* fibGenerator() {
const series = [0, 1];
while (true) {
const last = series[1];
const beforeLast = series.shift();
const next = last + beforeLast;
series.push(next);
yield next;
}
}
const getFibNumber = n => new Promise(resolve => {
const iterator = fibGenerator();
let i = 0;
(function computeCycle() {
i += 1;
const next = iterator.next().value;
console.info(`Compute progress: ${(i / n) * 100}%`);
if (i < n) {
window.requestIdleCallback(computeCycle);
} else {
resolve(next);
}
})();
});
// Example
getFibNumber(1000000).then(result => console.log(result));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment