Skip to content

Instantly share code, notes, and snippets.

@yuta0801
Last active January 15, 2020 15:57
Show Gist options
  • Save yuta0801/35fd6e4b38e761a4e3640b05f31cacff to your computer and use it in GitHub Desktop.
Save yuta0801/35fd6e4b38e761a4e3640b05f31cacff to your computer and use it in GitHub Desktop.
Fibonacci number implemented with recursive function async timer recursive function
// 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