I have been struggling to figure out why the HTTP requests are not been canceled using the switchMap operator.
import {Observable, of, Subject, Subscription, timer} from 'rxjs';
import {HttpClient} from '@angular/common/http';
constructor(private http: HttpClient) {}
ngOnInit(): void {
const makeRequest$ = new Subject();
const myResponse$ = makeRequest$.pipe(switchMap((id) => {
const url = `${environment.apiUrl()}/accounts?${id}`;
return this.http.get<any>(url, {
headers: {
'Content-Type': 'application/json'
}
});
}));
myResponse$.subscribe(d => console.log(d));
makeRequest$.next(1);
makeRequest$.next(-3);
makeRequest$.next(3); }
The following code will print only the response of the third call, but won't cancel the rest of the HTTP requests.
I have an interceptor for HTTP failure responses, which will fire an alert due to the second call with the wrong id.
When I am trying this code in stackblitz it WORKS and requests are been canceled. I am using Angular10.
Any setting that I am missing?