1. Obersvable and Observer are from RxJS similiar to Event and Event listener
  2. Observer can listen to three actions of observable that is next, error and complete
  3. Below we create a observer JSON key value pair and calling function based on the action from observable
  4. We need to subscribe to Observable in ourcase observable and pass observer as parameter
  5. We should unsubscribe to obeservable once we are done inorder to prevent memory leaks

test.html

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.0/Rx.js"></script>

    <script>
        var observer = {
            next: function (value) { console.log(value); },
            error: function (error) { console.log(error); },
            complete: function () { console.log("Completed..!") }
        };


        const observable = new Rx.Observable(obs => {
            obs.next(102);
            obs.next(103);
            setTimeout(function () {
                obs.next(109);
                obs.complete("Completed");
            }, 2000);
        });

        observable.subscribe(observer);
    </script>

Observable over Stream
Observable is similar to event which keeps happening. During its occurance it could emit values, throw error or comes to complete.

Comments are closed.