I have the following scenario.
If the users enter in the route with only /lobby without an ?competitionId= I must search on the state the favorite competition of the user. After receive the favorite competition of the user, i must redirect the user to /lobby?competitionId=XYZ.
I'm using the following code. Is a better way to implement ? It's looking like an callback hell to me. I'm using NGRX.
RxJS Stream Observsable Way
redirectToCompetition(): void {
this._store.pipe(select(routerSelectors.getState)).subscribe(routerState => {
if (!routerState.queryParams.competitionId) {
this._store
.pipe(select(authenticatedUserSelectors.getFavoriteCompetition))
.subscribe(favoriteCompetition => {
this._store.dispatch(
new routerActions.NavigateTo({
path: ['/lobby'],
query: { competitionId: favoriteCompetition.id }
})
);
});
}
});
}
I rewrite the code using async/await. It looks better.
Promise Async/Await Way
async redirectToCompetitionPromise() {
const routerState = await this._store
.pipe(select(routerSelectors.getState))
.first()
.toPromise();
const userFavoriteCompetition = await this._store
.pipe(select(authenticatedUserSelectors.getFavoriteCompetition))
.first()
.toPromise();
if (!routerState.queryParams.competitionId) {
this._store.dispatch(
new routerActions.NavigateTo({
path: ['/lobby'],
query: { competitionId: userFavoriteCompetition.id }
})
);
}
}
Any problem to use RxJS with async/await pattern ? Or in my first code i'm doing an bad implementation ?