New Observable Construction function - use first succesful observable #6069
Replies: 2 comments 1 reply
-
This issue is moved to discussions per its category. |
Beta Was this translation helpful? Give feedback.
-
I can see the issue you're coming across. Sadly, that doesn't do what you want either because you still don't know if all of them errored; although, you do. If you put const firstSuccess = (
...observables
) => (
from(
observables
.pipe(
catchError(() => (
EMPTY
)),
)
)
.pipe(
toArray(),
switchMap((
modifiedObservables,
) => (
race(
...modifiedObservables
)
))
)
.pipe(
endWith(null),
scan(
(
hasChange,
change,
) => (
(
!hasChange
&& change === null
)
? throw(new Error('All inner observables errored.'))
: true
),
false,
),
)
) Does this do what you want? I haven't tested it, but it sounds like what you want. |
Beta Was this translation helpful? Give feedback.
-
I am running into a situation where I need to try different versions of an API. Our vendor has different versions, occasionally the newer version of the API runs into availability issues and we need to fall-back to the slower (but functional) version.
I would like to abstract this away into an observable, preferably a top level function.
The use would be as such:
The behavior will be that A is subscribed to, until it emits an error. This error is swallowed, and B is subscribed to in succession. If it errors, C, D, E, etc. until the final subscription is made. This is like switchMap, but instead of switching when a new observable is entered, it switches when an error occurs, to the next item in the list.
Assuming all provided observables error out, the observable will
error
. Once one observable is finished, the entire thing completes.In this manner, we can choose the priority of which API version is used. In our case, the newer API would be used, but if there are issues, we would seamlessly fall back to the older, slower version.
My issue, is that
firstSuccess
is a crappy, crappy name. Is there something better I can call it? And would such a top-level function be useful to add to therxjs
project?Beta Was this translation helpful? Give feedback.
All reactions