Skip to content

Instantly share code, notes, and snippets.

@korniychuk
Created June 27, 2020 21:10
Show Gist options
  • Save korniychuk/870e3177af8027c0890ca7c2be35ab54 to your computer and use it in GitHub Desktop.
Save korniychuk/870e3177af8027c0890ca7c2be35ab54 to your computer and use it in GitHub Desktop.
Awaitable Observables :: Example 2
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