[RxJS] Creation operator: create()

We have been using Observable.create() a lot in previous lessons, so let‘s take a closer look how does it work.

The create function:

var foo = Rx.Observable.create( function(observer){
  observer.next(42);
  observer.next(100);
  observer.next(200);
  observer.complete();
})  ;

foo.subscribe(
  (x)=>{console.log(‘next ‘ + x);},
  (err)=>{console.log(‘err ‘ + err);},
  ()=>{console.log(‘done‘);},
)

In deep, create() function equal to new Rx.Observable():

var foo = new Rx.Observable( function(observer){
  observer.next(42);
  observer.next(100);
  observer.next(200);
  observer.complete();
})  ;

And this also equal to:

function subscribe(observer){
  observer.next(42);
  observer.next(100);
  observer.next(200);
  observer.complete();
}

var foo = new Rx.Observable( subscribe );

So, if we get rid of RxJS, then we can create the create() function like:

function subscribe(observer){
  observer.next(42);
  observer.next(100);
  observer.next(200);
  observer.complete();
}

var observer = {
  next: (x)=>{console.log(‘next ‘ + x);},
  error: (err)=>{console.log(‘err ‘ + err);},
  complete: ()=>{console.log(‘done‘);}
}

subscribe(observer);

Of course, it‘s useful to have the observable type because then it has all those nice operators that we saw and that we are also seeing new operators coming next. If you paid attention, then you‘re going to remember that in the subscribe, we had previously three functions here as argument. Instead of an object, as we have now, we had just these three functions.

Also, the observable type, it converts these three functions into an observer object. Before it calls this, it will actually take these three functions and put labels in front of them like that, to create the observer object. It‘s normalizing it.

时间: 2024-10-12 08:09:55

[RxJS] Creation operator: create()的相关文章

[RxJS] Creation operator: of()

RxJS is a lot about the so-called "operators". We will learn most of the important operators, one by one. In this lesson, we will see our first creation operator: of(). var foo = Rx.Observable.of(42, 100, 200); // var bar = Rx.Observable.create(

[RxJS] Connection operator: multicast and connect

We have seen how Subjects are useful for sharing an execution of an RxJS observable to multiple observers. However, this technique requires some laborious setting up. In this lesson we will learn about the multicast() operator which helps solve the s

[RxJS] Creation operators: from, fromArray, fromPromise

The of() operator essentially converted a list of arguments to an Observable. Since arrays are often how we structure list of things in JavaScript, we should have a way of transforming arrays into Observables. This lesson teaches how you can convert

[RxJS] Transformation operator: map and mapTo

We made our first operator called multiplyBy, which looks a bit useful, but in practice we don't need it because it's too specific: it only does simple multiplication on numbers. In this lesson we will see how the map() operator is useful for any cal

[RxJS] Utility operator: do

We just saw map which is a transformation operator. There are a couple of categories of operators, such as filtering, combination, flattening, etc. One of these categories is the utility operators. The most important utility operator is do, useful fo

[RxJS] Transformation operator: scan

All of the combination operators take two or more observables as input. These operators may also be alternatively called "vertical combination operators", because of how they work in a marble diagram. Next, we will learn about scan(), which is a

[RxJS] Combination operator: withLatestFrom

Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLatestFrom, another AND-style combination operator, and how it works essentially as map() operator, with some combination properties. var foo = Rx.Observa

[RxJS] Creation operators: empty, never, throw

This lesson introduces operators empty(), never(), and throw(), which despite being plain and void of functionality, are very useful when combining with other Observables. empty: var foo = Rx.Observable.empty(); // the same as var foo = Rx.Observable

[RxJS] Transformation operator: repeat

Operator repeat() is somewhat similar to retry(), but is not for handling operators. In this lesson we learn how repeat works. var foo = Rx.Observable.interval(500) .zip(Rx.Observable.of('a','b','c','d'), (x,y)=>y); var bar = foo.map(x => x.toUpperC