21

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.

wentjun
  • 40,384
  • 10
  • 95
  • 107
user2992476
  • 1,536
  • 2
  • 17
  • 29

1 Answers1

23

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.

wentjun
  • 40,384
  • 10
  • 95
  • 107