I have a polling use-case where:
- I want to call an API that, based on business logic, returns numbers(1-10) or error (network issue/ exception in API etc..) instantly (1.5-2 sec).
- If the API returns an error (network issue/ exception in API etc..) then I want to unsubscribe the polling and display error.
- If the API returns success, I want to check the return value and un-subscribe(if the return value is 5) or keep the polling going.
- I want to call API every 5 sec.
- I want to keep the max time(timeout/threshold) for the polling as 3mins. If I don't get the desired response(number 5) in these 3mins, the polling should error out.
This is how I have implemented it currently:
this.trackSpoke$ = interval(5000)
.pipe(
timeout(250000),
startWith(0),
switchMap(() =>
this.sharedService.pollForAPropertyValue(
"newuser"
)
)
)
.subscribe(
(data: SpokeProperty) => {
this.CheckSpokeStatus(data);
},
error => {
this.trackSpoke$.unsubscribe();
this.createSpokeState.CdhResponseTimedOut();
}
);
private CheckSpokeStatus(data) {
if (data.PropertyValue === "5") {
this.trackSpoke$.unsubscribe();
//display success
} else {
//keep the polling going
}
}
But, the above implementation is not timing out.
What needs to be done so that it times out and I am able to achieve all mentioned use-case?