I want to understand observables in a practical example. Assume I want to make API calls every 1 minute as long as the APP is on running state. Currently I am doing it using setInterval
function as below:
ngOnInit() {
setInterval(val => this.getDataFromAPI(), 60000)
}
getDataFromAPI() {
this.dataServices.getDataFromAPI().subscribe((data: any) => {
this.gaugeData = data;
},
error =>{
console.log('error')
});
}
In the dataServices
file there is a method 'getDataFromAPIwhich is responsible of making the
HTTP` calls.
getDataFromAPI
return this.http.get(this.base_url + this.endPoint, {
headers: {
'tokenid': this.tokenId,
'accountid': this.accountId'
}, params: { groupId: groupId, date: '2020-10-05T11:30:00-07:00' }
})
.toPromise().then(res => {
return res;
});
}
The above example is working very fine, but as mentioned in this article Understanding RxJS Observables and why you need them. they have a different way to create and use observables with subscriptions. How shall create and use observables in the example I provided?
You could use
timer
combined with amergeMap
.You already have this observable:
You will have to create an other observable, this will start an observable after 1s and he is gonna emit every 1s:
Create an empty subscription in your component, this will give you the ability to unsubscribe later:
Now you can make your subscription inside
ngOnInit()
:Don't forget to unsubscribe when you are finished, or you will have memory leaks:
I have a demo here if you want to check it out, search for
Custom API call
in the index.ts