You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 switchMap step 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:
All seems fine, right? switchMap is 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 async function above in which you turned a Promise into an Observable, you most certainly forgot to remove the async keyword, as well!!! Chances are it wasn't even the first time you did.
So, your switchMap step started returning a Promise<Observable<T>> instead of a Promise<T> or Observable<T>.
Hard luck, sure, one may argue; switchMap is 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 (and mergeMap, etc), when such a situation is detected?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
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