I am faced with the challenge of having multiple URLs containing crucial information. My goal is to find a specific ID within these URLs, but I do not know which URL holds the necessary details. The approach I'm taking involves calling each URL and using the first one that provides a valid response, while canceling the other calls.
Initially, I tried using the race()
function for this task. However, I encountered an issue where it would wait for the completion of the Observable, even if no values were emitted or they had been filtered out. To address this, I attempted to add .concat(never())
to each Observable. Unfortunately, this caused race()
to fail in canceling the Observables afterward, making them unusable for subsequent calls.
const urls = ['url1', 'url2', 'url3']
private getItem(id: string): Observable<string> {
return race(
...this.makeUrlCalls(id),
timer(10000).pipe(flatMap(() => of(''))),
)
}
private makeUrlCalls(id: string): Array<Observable<string>> {
return urls.map(url => {
return this.http.get<any>(url + id).pipe(
map(({items: [item]}) => item),
filter(Boolean),
concat(never()),
retry(2),
)
})
}
The desired outcome is to have the first URL that returns a valid value (non-empty array named item) be used from the race()
call.
Any assistance on solving this predicament would be highly valued!