Why Angular async pipe doesn't work with getter which returns Promise

Viewed 592

Component .ts

get user(): Promise<string> {
 //method returns the promise
return getCurrentUser();
  }

Component .html

<p>{{ user | async }}</p>

the expression above returns just an empty string in html

Why? What's the problem?

1 Answers

You should do something like:

Set a variable in ts class

public user: User;

in ngOnInit or other moment call the function that gets that user:

async fetchUser(): void {
 this.user = await getCurrentUser();
}

in the html only needed the following: <p>{{ user }}</p>

Since the variables are reactive, as soon the function updates the value of user, in the html will be updated.

You could also use observable as the official documentation uses. (the comment you got in the question)

Related