What is a pipe for rxJS

I think I have a basic concept, but there are some ambiguities

So, in general, this is how I use the observable:

observable.subscribe(x => {

})

If I want to filter the data, I can use this:

import { first, last, map, reduce, find, skipWhile } from 'rxjs/operators';
observable.pipe(
    map(x => {return x}),
    first()
    ).subscribe(x => {

})

I can also do this:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/first';

observable.map(x => {return x}).first().subscribe(x => {

})

So my questions are:

  • What is the difference?
  • If there is no difference, why is there a functional channel?
  • Why do these functions need different imports?
+55
source share
2 answers

The pipable statements (formerly lettable) are the current and recommended way to use statements starting with RxJS 5.5.

I highly recommend you read the official documentation https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md

, , Observable , .

import 'rxjs/add/operator/first' . RxJS, . , 'rxjs/add/operator/first' . , .

+51

, . , RxJS 5.5 pipe , , pipe :

?

, , , rxjs/operators. , , , , .

const { Observable } = require('rxjs/Rx')
const { filter, map, reduce,  } = require('rxjs/operators')
const { pipe } = require('rxjs/Rx')

const filterOutEvens = filter(x => x % 2)
const doubleBy = x => map(value => value * x);
const sum = reduce((acc, next) => acc + next, 0);
const source$ = Observable.range(0, 10)

source$.pipe(
  filterOutEvens, 
  doubleBy(2), 
  sum)
  .subscribe(console.log); // 50
0

Source: https://habr.com/ru/post/1693326/


All Articles