How to simplify multiple async pipes
Tags: angular, javascript, frontend
Published: 2022-03-20 12:08:00

In Angular is very common suscribe to multiple observables to show data in our template, and use these observables in our template we use multiple async pipe.

> async pipe, make easy to subscribe and unsubscribe from the observable in our templates.

For example, our app shows the user's name and the player stats, each of them came from another api.

```typescript
  playerNumber = 237;
 player$ = this.nbaService.getPlayer(this.playerNumber);
  stats$ = this.nbaService.getStats(this.playerNumber);
```

The template looks like: 

```html
  <div *ngIf="player$ | async as player" class="player">
    <h2>{{ player.first_name }} {{ player.last_name }}</h2>
    <h3>Stats</h3>
    <ul *ngIf="stats$ | async as stats">
      <li *ngFor="let stat of stats.data">
        Points: {{ stat.pts }} Rebounds: {{ stat.reb }} Steals: {{ stat.stl }}
      </li>
    </ul>
  </div>
```

How can combine our observable into a single observable? 

Rxjs provide [combineLatest](https://rxjs.dev/api/index/function/combineLatest), it returns an array of each observable.

[CombineLatest](https://rxjs.dev/api/index/function/combineLatest) only emit until all observable emit one value, we want to show when the player$ and stats$ emit a value.

Create a new observable like player$ and it will contain properties for each observable,

Pipe the values from combineLatest, pipe them with map to return an object with clean name about each value to use in the template.

```typescript
  playerData$ = combineLatest([this.player$, this.stats$]).pipe(
    map(([info, stats]) => ({ info, stats }))
  );
```
Update the template to use  the pipe only for the playerData , remove the ngIf and extra async pipe.

```html
<div class="container">
  <h1>Nba</h1>
  <div *ngIf="playerData$ | async as playerData">
    <h2>{{ playerData.info.first_name }} {{ playerData.info.last_name }}</h2>
    <h3>Stats</h3>
    <ul>
      <li *ngFor="let stat of playerData.stats.data">
        Points: {{ stat.pts }} Rebounds: {{ stat.reb }} Steals: {{ stat.stl }}
      </li>
    </ul>
  </div>
</div>
```
We have a single observable to manage both subscriptions. Use combineLatest to merge and combine the data and use the template.

## Part II, Improving the code  

Thanks to @layzee for the feedback, we can improve the code using:

- Use a presentational component user-profile
- Convert the app component into a container component to deal with the observable process and process data.

> Read more about [Container Components](https://dev.to/this-is-angular/container-components-with-angular-4o05) 
> Read more about [presentational components](https://dev.to/this-is-angular/presentational-components-with-angular-3961)

### Create presentational component player-profile

We create the component app-player-info only to show the data using input properties.

```typescript
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-player-info',
  templateUrl: './player-info.component.html',
  styleUrls: ['./player-info.component.css'],
})
export class PlayerInfoComponent {
  @Input() name: string;
  @Input() stats: any;
}
```

The app.component process the data in the observable using the map rxjs operator to simplify stats.data array to a single object using destructuring.

> Read more about [Rxjs map operator](https://rxjs.dev/api/operators/map).
> Read more about [destructuring object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment).

```typescript
 stats$ = this.nbaService.getStats(this.playerNumber).pipe(
    map((value) => {
      return {
        ...value.data[0],
      };
    })
  );
```

Edit the template, use the player-profile component and bind the properties.

```html
<div class="container">
  <h1>Nba</h1>
  <div *ngIf="playerData$ | async as player">
    <app-player-info
      [name]="player.info.first_name"
      [stats]="player.stats"
    ></app-player-info>
  </div>
</div>
```
Our code has a separation about processing the data and showing the information.

Feel free to play with the [demo](https://stackblitz.com/edit/angular-ivy-tzicsk?file=src%2Fapp%2Fapp.component.html)

Photo by Michał Parzuchowski on Unsplash