Observable and Observer

  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

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>
我来评几句
登录后评论

已发表评论数()

相关站点

热门文章