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
Hi,
We recently migrated our rxjs package version in our project. Part of migration was replacing toPromise deprecated function with first/lastValueFrom function. A problem that was raised is the throw of an EmptyError due to the fact the some of our functions immediately returns an EMPTY observable, some of our functions are just an api calls that return EMPTY observable from the catchError operator:
Example:
exportfunctionappInitializer(authService: AuthService,serviceA: ServiceA,serviceB: ServiceB){//when user not logged in refreshToken return an EMPTY observable which will make the app throw EmptyError.//We could make refreshToken return observable of undefined using of(undefined) but it's seems weird and less readable//Using defaultValue is possible but it's not what I like to call screaming code, the next developer will know to use it//only if he will check the entire upstream to see if an EMPTY observable might be returned to understand why defaultValue was //usedconsttokenPromise=lastValueFrom(authService.refreshToken())//When either of the call will fail the error wont be ignored as catchError will return an EMPTY observable before//any value has been emitted, causing EmptyError to be thrown//Same here, we could also make catchError return observable of undefined for example using of(undefined) but seems less//readableconstnonFatalPromise=lastValueFrom(combineLatest([serviceA.loadDataFromApiCall().pipe(catchError(error=>EMPTY)),serviceB.loadDataFromApiCall().pipe(catchError(error=>EMPTY))]))return()=>newPromise<void>(resolve=>{Promise.all([tokenPromise,nonFatalPromise]).then(()=>{resolve();})});}exportclassAuthService{refreshToken(){if(isLoggedIn){returnapi.refreshToken()}returnEMPTY}}
This scenarios made me come up with 2 questions:
What is the right approach to handle EMPTY observable scenario from upstream when using first/lastValueFrom so that toPromise deprecated function behavior will be saved
Shouldn't the api be cooperative as it was using with toPromise function? Now everywhere that an EMPTY obsrevable is returned from the upstream pipe that error might occur
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
-
Hi,
We recently migrated our rxjs package version in our project. Part of migration was replacing toPromise deprecated function with first/lastValueFrom function. A problem that was raised is the throw of an EmptyError due to the fact the some of our functions immediately returns an EMPTY observable, some of our functions are just an api calls that return EMPTY observable from the catchError operator:
Example:
This scenarios made me come up with 2 questions:
Beta Was this translation helpful? Give feedback.
All reactions