Store first value of http request with rxjs but keep doing httprequest With RXJS library :
I need to store values from the FIRST http request, but keep doing other http request on demand.

I have done this code so far, and that is working. But I think there is a more elegant way.

const Rx = require("rxjs");
let numHttp = 0;
const httpRequest = () =&gt; {
    let a = [];
    numHttp = numHttp + 1;
    for (let i = 0; i &lt; 10; ++i) {
        a.push({ num: i, http: numHttp });
    };
    return new Rx.BehaviorSubject(a)
        .do(a =&gt; {
            console.log("antoher http request");
            if (getC.getValue() === undefined) {
                getC.next(a)
            }
        })
        // .do(n =&gt; { if (getB.) getB.next(n) })
}
const getB = () =&gt; {
    const a = new Rx.BehaviorSubject();
    return a
};
getC = getB();
httpRequest().subscribe(d =&gt; d);
httpRequest().subscribe(d =&gt; d);
getC.subscribe(data =&gt; console.log('cache', data))
httpRequest().subscribe(d =&gt; d);
httpRequest().subscribe(d =&gt; d);
It is really hard to solve the problem since the question is not providing all the necessary details.

Assuming you have a factory for your Http observable (call it createHttpObservable()) you may do something like this:

const observable = createHttpObservable()

  // Share the stream of events across all the subscribers.
  .publishReplay(1)
  .refCount()

  // Use `.scan()` against event stream (similarly to how we `.reduce()` against an array).
  .scan(
    (history, currentValue, valueIndex) =&gt; {
      history.push({ num: valueIndex, httpNum: currentValue });
      return history;
    },
    []
  )

  // Take the last event from the `history`
  .map(history =&gt; history[history.length - 1].httpNum)

  // Arbitrarily use `.do` for side effects.
  .do(console.log);


// Subscribe wherever you need.
observable.subscribe();
observable.subscribe();
observable.subscribe();


Here's a related, but not quite the same exact plunkr.

You can have more custom logic in the .scan() block, but frankly, I have no idea what are you trying to achieve and why is the first Http request's results are of any special interest.