Having the following (just a quick example):
observable.pipe(map( s => s.anything ))
.pipe(filter(t => t > 5))
.pipe(map( t => t+5))
.subscribe( XXX )
Why should I use 1 pipe instead?
observable.pipe(
map( s => s.anything ), filter(t => t > 5),map( t => t+5))
.subscribe( XXX )
To me, the code is more nice and readable in the first case. But no idea if that affects the behaviour anyway.
The pipe method of the Angular Observable is used to chain multiple operators together. We can use the pipe as a standalone method, which helps us to reuse it at multiple places or as an instance method.
Async pipelinkThe AsyncPipe subscribes to an observable or promise and returns the latest value it has emitted.
A Pipeable Operator is essentially a pure function which takes one Observable as input and generates another Observable as output. Subscribing to the output Observable will also subscribe to the input Observable.
You should use a single pipe for that purpose. Having multiple pipes will serve no purpose or benefit, as the usage of pipes is to combine RxJS functional operators into a single chain.
In order to make it more readable, you can do something like this, instead of having all the operators on 1 single line of code.
observable
.pipe(
map(s => s.anything ),
filter(t => t > 5),
map(t => t+5)
).subscribe(res => {
// do the rest
});
The official Angular guide has a good summary on the usage of pipes and other operators. You may read up more about it over here. You should read up about pipeable operators over here as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With