What is the correct way to share the result of an Angular Http network call in RxJs 5?

前端 未结 21 1371
广开言路
广开言路 2020-11-21 06:11

By using Http, we call a method that does a network call and returns an http observable:

getCustomer() {
    return          


        
21条回答
  •  青春惊慌失措
    2020-11-21 06:23

    Have you tried running the code you already have?

    Because you are constructing the Observable from the promise resulting from getJSON(), the network request is made before anyone subscribes. And the resulting promise is shared by all subscribers.

    var promise = jQuery.getJSON(requestUrl); // network call is executed now
    var o = Rx.Observable.fromPromise(promise); // just wraps it in an observable
    o.subscribe(...); // does not trigger network call
    o.subscribe(...); // does not trigger network call
    // ...
    

提交回复
热议问题