My question headline may not be very clear, let me clarify more and you are welcome to improve it.
I have an angular application which polls a set of data every few seconds. Now I have a timer observable like this, and is subscribed in another place.
public readonly people$ = timer(3, 10000).pipe(
switchMap(()=> this.http.get(url).pipe(tap(()=>{
//here is some business logic
//then at the end here I want to send a request unrelated to main business logic flow, e.g. auditing information update`
})
)
)
I need this people$ observable to ONLY return people at the end, but meanwhile to trigger another http request to do something else that's unrelated to current stream.
What I have tried:
- Calling
http.post(anotherUrl)inside thetapbut obviously it won't work because it's not subscribed.
public readonly people$ = timer(3, 10000).pipe(
switchMap(()=> this.http.get(url).pipe(
tap(()=>{
//here is some business logic
this.post(anotherUrl); //won't trigger as no subscription and I don't think it's good to subscribe here
})
)
)
- Changing
taptoasyncand calling the request withtoPromise, like:
public readonly people$ = timer(3, 10000).pipe(
switchMap(()=> this.http.get(url).pipe(
tap(async()=>{
//here is some business logic
await this.post(anotherUrl).toPromise();
})
)
)
And this can work, but I wonder what should be a proper way to do this and whether I can achieve this in a rxjs way?