Skip to content

Instantly share code, notes, and snippets.

@Vishwas1
Last active July 8, 2019 04:29
Show Gist options
  • Save Vishwas1/3883518ba6541b70b6080de68754556a to your computer and use it in GitHub Desktop.
Save Vishwas1/3883518ba6541b70b6080de68754556a to your computer and use it in GitHub Desktop.
Reactive programming polling
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