Last active
January 15, 2020 15:57
-
-
Save yuta0801/35fd6e4b38e761a4e3640b05f31cacff to your computer and use it in GitHub Desktop.
Fibonacci number implemented with recursive function async timer recursive function
This file contains 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
// Utility function to make fake async processing | |
const delay = fn => new Promise(r => setTimeout(() => r(fn()), 100)) | |
const isConstant = n => delay(() => n < 2) | |
const fibo = async n => { | |
if (await isConstant(n)) return n === 0 ? 0 : 1 | |
const [prev1, prev2] = await Promise.all([fibo(n - 1), fibo(n - 2)]) | |
return delay(() => prev1 + prev2) | |
} | |
fibo(10).then(n => console.log(n)) // 55 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment