Created
June 27, 2020 21:10
-
-
Save korniychuk/870e3177af8027c0890ca7c2be35ab54 to your computer and use it in GitHub Desktop.
Awaitable Observables :: Example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
@Component({ /* ... */ }) | |
export class MyComponent implements OnInit { | |
public readonly currentUser$: Observable<User | undefined> = this.store.select(fromStore.getCurrentUser); | |
public constructor( | |
private readonly store: Store<AppState>, | |
private readonly my: MyService, | |
) {} | |
public async doSomethingIfUserDefinedViaRxJS(): Promise<void> { | |
this.currentUser$ | |
.pipe( | |
first(), | |
filter((user?: User) => !!v), | |
) | |
.subscribe((user: User) => /* ... */); | |
} | |
public async doSomethingIfUserDefined(): Promise<void> { | |
if (!await this.currentUser$) return; | |
// ... | |
} | |
public async doSomethingWithCurrentUserViaRxJS(): Promise<void> { | |
this.currentUser$ | |
.pipe(first()) | |
.subscribe(user => this.my.doSomething(user)); | |
} | |
public async doSomethingWithCurrentUser(): Promise<void> { | |
this.my.doSomething(await this.currentUser$); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment