Last active
July 8, 2019 04:29
-
-
Save Vishwas1/3883518ba6541b70b6080de68754556a to your computer and use it in GitHub Desktop.
Reactive programming polling
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
const { from, timer } = require('rxjs'); | |
const fetch = require('node-fetch'); | |
const getTimeFromAPI = () => { | |
return fetch('http://worldclockapi.com/api/json/est/now') | |
.then((resp) => resp.json()) | |
} | |
// Observer timer and subscribe | |
// emit first value after 1sec and subsequent values after every 3sec | |
timer(1000, 3000) // timer observable | |
.subscribe( | |
_ => { | |
// observe API and subscribe to it | |
from(getTimeFromAPI()) // promise observable | |
.subscribe((data) => { | |
console.log('-------------------------------------------') | |
console.log(data.currentFileTime) | |
}) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment