Rx JS Subscribe to an observer for several observations

Scratching the surface of the Rx JS, I got the following snippet:

    var observer1 = Rx.Observer.create(
         function (x) {
             console.log('Next: ' + x);
         },
         function (err) {
             console.log('Error: ' + err);
         },
         function () {
             console.log('Completed');
         }
     );  

     var observer2 = Rx.Observer.create(
         function (x) {
             console.log('Next: ' + x); 
         },  
         function (err) {
             console.log('Error: ' + err);   
         },  
         function () {
             console.log('Completed');   
         }   
     );  


     var source1 = Rx.Observable.return(1);
     var source2 = Rx.Observable.return(2);

     var subscription1 = source1.subscribe(observer1);
     var subscription2 = source2.subscribe(observer1);

CONCLUSION: Next: 1 Completed

JS BIN Code Link: http://goo.gl/DiHdWu

Signing the same observer for both streams gives only data from the first. However, when subscribing to another observer, everything goes as expected. Can someone explain what is happening?

     var subscription1 = source1.subscribe(observer1);
     var subscription2 = source2.subscribe(observer2);

CONCLUSION: Next: 1 Completed Next: 2 Completed

+4
source share
1 answer

, Observables, , . Merge, concat. jsbin.

?

IObserver Observer.create. OnNext OnError OnComplete.

Observables, , / ( , OnError/OnCompleted), - . , .

, , Merge, concat, (OnError/OnCompleted) , .

//Triggers observer1 for both observables(source1 & source2)
var subscription = source1.concat(source2).subscribe(observer1);

//Triggers observer2 for both observables(source1 & source2)
var subscription = source1.merge(source2).subscribe(observer2);
+4

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


All Articles