switchMap not double-flattening its internals #7596
dariomannu
started this conversation in
Ideas / Feature request
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Whether this is a bug is open to debate (more like a subtle GOTCHA, in fact), but it's been causing a few inglorious debugging parades that may be worth some discussion.
The journey starts with a
switchMapstep that makes a humble fetch call, as we rely on the fact that Promises, as well as async functions, normally get flatMapped back into Observables, so we can conveniently do the following:What happens in real life is you start refactoring and perhaps you change the internals of the above step, replacing your promise with an Observable:
All seems fine, right?
switchMapis known to flatten internal streams, as well...Nope, what happens instead is you notice some really weird behaviour first, spend a few hours deep debugging, wondering why are you suddenly getting an observable somewhere else in your app when you were expecting a value.
Turns out, whilst refactoring that
asyncfunction above in which you turned a Promise into an Observable, you most certainly forgot to remove theasynckeyword, as well!!!Chances are it wasn't even the first time you did.
So, your
switchMapstep started returning aPromise<Observable<T>>instead of aPromise<T>orObservable<T>.Hard luck, sure, one may argue;
switchMapis working as expected by performing ONE flattening step, as designed.Solutions?
Double-flattening doesn't look like the most elegant/viable/acceptable solution, but at least we may have the chance to make some improvements?
Perhaps raising a warning from
switchMap(andmergeMap, etc), when such a situation is detected?Anybody ever been in this situation?
Beta Was this translation helpful? Give feedback.
All reactions