- Obersvable and Observer are from RxJS similiar to Event and Event listener
- Observer can listen to three actions of observable that is next, error and complete
- Below we create a observer JSON key value pair and calling function based on the action from observable
- We need to subscribe to Observable in ourcase observable and pass observer as parameter
- 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.