I'm struggling a bit with coming up with a good solution for doing multiple http requests, based on the result of each of them. So far, I've been doing good with switchMap, as long as it's only 2 http requests. However, I've found out I need to perform 3, and I'm not sure how to do it with switchMap, or if there's a better solution for it.
Basically, I will await a response, and if it comes back empty, I want to perform a 2nd request, and if that comes back empty, I want to perform a 3rd request.
Let me try to give a simplified example of what I've gotten so far:
getData(): Observable<number> {
const $subject = new Subject<number>();
// loadData() takes in an id to specify what data to load
service.loadData(1).pipe(
switchMap(response => {
if(response.length === 0) {
return service.loadData(2);
}
else {
return of(response);
}
}),
).subscribe(result => {
$subject.next(result);
$subject.complete();
});
return $subject.asObservable();
Now this works just fine, my issue is, that calling "loadData(2)", might also give back a response with length = 0, and in that case, I need to call loadData(3).
I tried having another switchMap within the switchMap, but that didn't seem to work properly. So my question is, how would I be able to check the length of the response of loadData(2), and if it's 0, return service.loadData(3) ?
Any help or tips are very welcome :)