0

On the way of understanding Observable, Observer and subscribe I just came to this example and unable to understand this example and I'm confuse

var observable = Rx.Observable.create(
 function(observer) {     
 observer.onNext('Simon');
 observer.onNext('Jen');  
 observer.onNext('Sergi');
 observer.onCompleted();  // We are done }
);

whats happening here? what we are creating here stream of observer? but observer is the one who receives data from stream when subscribing to the stream with subscribe method and have 3 methods onNext, complete and error.

Also give me example. where we create a stream, a observer(standalone) and how this standalone observer subscribe to a observable.

blackHawk
  • 6,047
  • 13
  • 57
  • 100

1 Answers1

0

The documentation is your entry door to understanding observables and their mechanics. Now the truth is that it can be pretty dense some times. I would recommend you have a look here :

The short answer to your questions is that :

  • Rx.Observable.create returns an object (an observable) which implements an interface (described in the documentation) which includes a subscribe function. When you execute that subscribe function as in .subscribe(observer), the factory function you passed as parameter of Rx.Observable.create is executed with that observer parameter. In your case, Simon etc. values will be pushed to the observer (which implements the observer interface which includes onNext, onCompleted etc.).
  • Generally the overload form .subscribe(function yournamehere(){...}) is used. Hence an observer is made internally from that function, with its onNext being the function you passed, and its onError and onCompleted being default values.
  • For the second part of your question, cf. documentation https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/observer.md
Community
  • 1
  • 1
user3743222
  • 18,345
  • 5
  • 69
  • 75
  • I have been reading documentation I di=ont understand this: "the Create operator of the Observer type, which creates and returns an observer from specified OnNext, OnError, and OnCompleted action delegates." from your very first given link – blackHawk Jun 01 '16 at 12:41
  • I am sorry I cannot provide you much more help. The sentence is pretty clear and there is an example right next to it. I could only repeat what is there. – user3743222 Jun 01 '16 at 14:33
  • urmm you can shape my thoughts on it either its wrong or right, from my understanding create accept a function and create stream of data that function returning, in my case(the stream in the question) the author instead of giving a function that return some data, used a function with onNext method(observer mayb?) why there is onNext() – blackHawk Jun 01 '16 at 15:25