Skip to content

Filtered getItem seems to be emitting something #44

Discussion options

You must be logged in to vote

When there is a token already: race will take the first Observable to emit, no matter the order. But the order is important in what you want: 1/ queryParams or 2/ localStorage or 3/ default value. So you want to chain, not to race.

When there is not a token yet: I suspect that even if you filter when there is no token, the localStorage Observable completes (and that is normal), and then win the race. But all of this is due to the previous point: you race instead of chaining.

You want something like this:

of(this.route.snapshot.queryParams.get('token')).pipe(
  switchMap((token) => token ? of(token) : this.localStorage.getItem('token')),
  map((token) => token ? token : 'NO_TOKEN')
);

Clos…

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by cyrilletuzi
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #44 on December 08, 2020 21:31.