Pipes chain in Angular — using async pipe

You can find many examples of pipes working, but there is not enough information on how to use the asynchronous pipe in the chain. If you want to know everything about pipes, please read this article:
So let’s start.
I want to show you how a pipe chain works without an async pipe and then we can compare two ways.
First of all, we have to create two pipes.
Get-length.pipe.ts will return the length of the string.
//get-length.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'getLength'
})
export class GetLengthPipe implements PipeTransform {
transform(value: string, ...args: unknown[]): number {
return value.length;
}
}Mark.pipe.ts will return HTML element with the marked background if the length is more than 3.
// mark.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'mark'
})
export class MarkPipe implements PipeTransform {
transform(value: number, ...args: unknown[]): string {
if (value > 3){
return `<span class="marked">${value}</span>`
} else {
return `<span>${value}</span>`
}
}
}
Next, we have to create app.component.html with input and pipes chain and app.component.ts.
//app.component.html
<h5>Pipes chain</h5>
<input [(ngModel)]="input">
<p [innerHTML]="input | getLength | mark"></p>
//app.component.ts
import {Component} from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
public input: string;
constructor() {
this.input = '';
}
}
But if you need to wait for data from your server or another async function you need to use async pipe.
Theasyncpipe subscribes to anObservableorPromiseand returns the latest value it has emitted. When a new value is emitted, theasyncpipe marks the component to be checked for changes. When the component gets destroyed, theasyncpipe unsubscribes automatically to avoid potential memory leaks. When the reference of the expression changes, theasyncpipe automatically unsubscribes from the oldObservableorPromiseand subscribes to the new one.
For example, we will wait for response from the server and mark users length:
//app.component.ts
import {Component} from '@angular/core';
import {HttpClient} from "@angular/common/http";
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
public input: any;
constructor(private http: HttpClient) {
this.input = this.http.get(`http://localhost:3001/users`)
}
}Now we have to change our app.component.html:
//app.component.html
<h5>Pipes chain</h5>
<input [(ngModel)]="input">
<p [innerHTML]="input | async | getLength | mark as users"></p>
Advantages of this way:
1. You don’t have to use subscribe/unsubscribe methods.
2. Your code is shorter and simpler.
3. You increase your app performance.
And that’s all. You can use this solution for all promises or observables.
Thanks for reading, if you like this article you can 👏 or write a comment.
See also other articles about Angular features: