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 = () => {
    let a = [];
    numHttp = numHttp + 1;
    for (let i = 0; i < 10; ++i) {
        a.push({ num: i, http: numHttp });
    return new Rx.BehaviorSubject(a)
        .do(a => {
            console.log("antoher http request");
            if (getC.getValue() === undefined) {
        // .do(n => { if (getB.) getB.next(n) })
const getB = () => {
    const a = new Rx.BehaviorSubject();
    return a
getC = getB();
httpRequest().subscribe(d => d);
httpRequest().subscribe(d => d);
getC.subscribe(data => console.log('cache', data))
httpRequest().subscribe(d => d);
httpRequest().subscribe(d => d);

1 Answer 1


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.

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

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

  // Arbitrarily use `.do` for side effects.

// Subscribe wherever you need.

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.