Skip to content

Instantly share code, notes, and snippets.

@crunchie84
Last active September 29, 2020 05:21
Show Gist options
  • Save crunchie84/45728a2314fa953cf474e30f355161cf to your computer and use it in GitHub Desktop.
Save crunchie84/45728a2314fa953cf474e30f355161cf to your computer and use it in GitHub Desktop.
RxJs bitcoin stock ticker example
Rx.Observable.timer(0, 10 * 1000)
.switchMap(_ => fetch('https://blockchain.info/ticker?cors=true')
.then(res => res.json())
.then(parsed => parsed.EUR.buy)
)
.distinctUntilChanged()
.scan((acc, curr) => ({
delta: Math.round((acc.value - curr || 0) * 100) / 100,
value: curr
}), {})
.subscribe(tick => console.log(`1 BTC = ${tick.value} EUR (Δ ${tick.delta > 0 ? '+' : ''}${tick.delta})`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment