Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use the pipe operator if I only use one operator?

RxJs version 5.5 introduced the pipe operator to make it easier to combine RxJs operators and to make tree shaking more efficient for these situations. My question is, should you use the pipe operator if you only intend to use one operator?

Consider the following examples:

someObservable.map(mappingLogicMethod).subscribe(x => console.log(x));

vs

someObservable.pipe(map(mappingLogicMethod)).subscribe(x => console.log(x));

In situations such as this where you only use one operator what is the most appropriate approach?

like image 294
João Paiva Avatar asked Feb 05 '19 16:02

João Paiva


1 Answers

The short answer is "You can skip the pipe, but you shouldn't" because the pipe operator is not mandatory prior to version 6.0 as mentioned by martin in the comments (so version < 6 is not a must), and if you include rxjs-compact you can use the old way of chaining operators (so you can actually use the old way everywhere up to the latest 6. version).

Lets dive a bit on the part: "Why you shouldn't use it"

Since version 5.5 the pipe operator is avaliable (but not mandatory) for usage and is usually preferred, because (back then) we all knew that in the next versions all operators will be wrapped in pipe , so you can think about that period 5.5<=6 like a migration window, to the new way.

Although we are now at version 6+ and the usage of pipe is the default behaviour, rxjs still supports the old chaining (no pipe) as long as you also install rxjs-compat.

So the answer is: If you are using rxjs 5.5+, and you want to make use of tree shaking and write readable code by the new standard (as mentioned by cartant in the comments)?

"You must always use pipe".

like image 118
Християн Христов Avatar answered Oct 10 '22 21:10

Християн Христов