RxJS implements the reactive programming paradigm on the core concept of OBSERVABLES.
If you already know javascript's Promises, the observables have some slight differences:
- a Promise is eager, whereas an Observable is lazy;
- a Promise is always asynchronous, while an Observable can be either synchronous or asynchronous;
- a Promise can provide a single value, whereas an Observable is a stream of values (from 0 to multiple values);
Having said this, RxJS libraries provides a lot of function named Creation Operators
which are functions that can be used to create new OBSERVABLES starting from very common JS behaviour
for example AJAX calls (asynchronous javascript and xml)
import { ajax } from 'rxjs/ajax';
// Create an Observable that will create an AJAX request
const apiData = ajax('/api/data');
On this Kind of operators, we can apply Pipeable Operators.
The difference among them is that they have to be applied on an existing operator,
creating a new one.
Imagining the concatenation of a series of this pipeable operators, the code will be written as:
pipeableOperator3(pipeableOperator2(pipeableOperator(creatorOperator())))
For that reason, Observables have a method called .pipe() that accomplishes the same thing while being much easier to read:
creatorOperator().pipe(
pipeableOperator1(),
pipeableOperator2(),
pipeableOperator3());