Dany Paredes
Dany Paredes | Javascript / Web

Dany Paredes | Javascript / Web

How to simplify multiple async pipes

Photo by Michał Parzuchowski on Unsplash

How to simplify multiple async pipes

Reduce the async pipe in the template

Dany Paredes's photo
Dany Paredes
·Mar 20, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

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 show the user name and the player stats, each of them came from another api.

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

The template looks like:

  <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, it return a array of each observable.

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 a object with clean name about each value to use in the template.

  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.

<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 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 Read more about presentational components

Create presentational component player-profile

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

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. Read more about destructuring object.

 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.

<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

Photo by Michał Parzuchowski on Unsplash

Did you find this article valuable?

Support Dany Paredes by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this