Created
February 19, 2018 00:20
-
-
Save codyromano/fc65690878ee6ceaf42a85f70715c00c to your computer and use it in GitHub Desktop.
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* 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