What is the correct way to share the result of an Angular Http network call in RxJs 5?
<p>By using Http, we call a method that does a network call and returns an http observable:</p>



<pre class="lang-js prettyprint-override"><code>getCustomer() {
    return this.http.get('/someUrl').map(res =&gt; res.json());
}
</code></pre>

<p>If we take this observable and add multiple subscribers to it:</p>

<pre class="lang-js prettyprint-override"><code>let network$ = getCustomer();

let subscriber1 = network$.subscribe(...);
let subscriber2 = network$.subscribe(...);
</code></pre>

<p>What we want to do, is ensure that this does not cause multiple network requests.</p>

<p>This might seem like an unusual scenario, but its actually quite common: for example if the caller subscribes to the observable to display an error message, and passes it to the template using the async pipe, we already have two subscribers.</p>

<p>What is the correct way of doing that in RxJs 5? </p>

<p>Namely, this seems to work fine:</p>

<pre class="lang-js prettyprint-override"><code>getCustomer() {
    return this.http.get('/someUrl').map(res =&gt; res.json()).share();
}
</code></pre>

<p>But is this the idiomatic way of doing this in RxJs 5, or should we do something else instead? </p>

<p><em>Note : As per Angular 5 new <code>HttpClient</code>, the <code>.map(res =&gt; res.json())</code> part in all examples is now useless, as JSON result is now assumed by default.</em></p>