This is the second part of a small series, in which I want to share some pitfalls we discovered multiple times in code reviews over the years, as well a few patterns we identified as helpful. In the first part we discussed how nesting subscriptions can be harmful.

Stateful Streams in Use

Often when modeling our streams with RxJS, we end up in a situation where we need to switch our streams but need to keep their results together. Naturally, the need to store the intermediate result emerges. A common attempt to solve this problem we encountered numerous times would be to store these intermediate results outside of our stream. In the case of using Angular, the destination often tends to be the component.

  switchMap(({ customerId }) => customerService.getCustomer(customerId)),
  tap((customer) => { 
    this.customer = customer;
    this.code = makeCode(customer);
  switchMap(() => myService.retrieveByCode(this.code)),
  tap((result) => { this.result = result; }),
  switchMap(() => otherService.byCustomerAndResult(this.customer, this.result)),
).subscribe(combinedResult => {
  this.result = combinedResult;
  this.view = moreComplexComutation(this.customer, this.code, combinedresult);

In the above implementation we react to the change of a route param customerId. Once it changes, we switchMap to request an customer object from our API. In the following tap, we store the intermediate results on our component. We then switchMap to hit our API again using the previously stored this.code and use the following tap to once again store this intermediate result on our component. The next switchMap also requests further information from our API and finally all intermediate results are combined and the view is updated in our subscribe.

The Bad

At every step of the pipeline we are relying on state that is kept outside of our stream. This can introduce side-effects and we cannot argue about our data flow easily, as we cannot know at what point in time which property will be updated, thus making our stream rely on state.

Imagine there is some effect or action outside of the stream mutating these values. It is not obvious to the developer looking at the code that there might actually be something tampering with the state while the stream is executing.

What if one of the requests fails? Then our state will go out of sync and maybe even produce erroneous results. In order to prevent this, proper error handling becomes a hassle, as we have to handle every request separately and perform the appropriate cleanup.

The Fix

The need for state arose from the need to store intermediate results that can be picked up at a later stage of our pipeline. We now want to introduce a way to model our streams that alleviates the need to store results outside of the stream and rather carry them along through our pipeline.

// Emitting customerIds 1 and 2 with a 25ms delay
createStream<number>([1, 2], 25)
    // Switch to request customer object
    switchMap(id => requestCustomer(id)),
    // Switch to request made by generating the customer code
    switchMap((customer) => {
      const code = makeCode(customer);

      return requestByCode(code)
        // Combine results into a bundle
        .pipe(map(result => ({ customer, code, result })));
    // Switch to final request
    switchMap(({ customer, result, ...bundle }) => {
      return requestByCustomerAndResult(customer, result)
        // Add the new result to the bundle
        .pipe(map(combinedResult => ({ ...bundle, customer, result, combinedResult })));
  .subscribe(({ customer, code, result, combinedResult }) => {
    // Update view or properties all at once
    updateView('code', code);
    updateView('result', result.result);
    updateView('combined', combinedResult);

In the snippet above, we react to our stream emitting customer ids. We then switchMap to request the corresponding customer object. Once retrieved, we compute the code and switchMap to request more data from our API. At this point we use map to transform the response to a bundle containing the customer, our code and the result of our request. From this bundle, we use what we need (customer and result) to finally switchMap to our final request. Once retrieved, we once again combine all of our results into a bundle. Finally, we have all the data we need to update our view accordingly in a single step, making sure nothing can go out of sync.

Please note how the use of destructuring helps us accessing the parts of our bundles easily.

You can find a running example on stackblitz.


In this post we have discussed how keeping state outside of our stream can potentially introduce unforeseen side-effects. We introduced a simple way to remove the need for storing intermediate results, which keeps our data flow simple and once again makes it easier to argue about how our data flows. This also increases the readability for fellow developers as they just have to follow the pipeline and not worry about a state that could potentially be manipulated from outside of the stream.

In the next part of the series we show how employing the techniques introduced can make working with RxJS in Angular applications easier for us.

Sign up for our dev newsletter, and don't miss out on new articles, screencasts, webinars, and more from our experts.

Related Articles

 | Yannick Baron

In this third part of the series we want to talk about another advantage when modeling our streams in this manner, specifically in the context of an Angular application. Composing Data Streams to Yield a Single Result and Leveraging the Async Pipe Similar to the previous posts…

Read article
 | Yannick Baron

In this first part of a small series, I want to share some pitfalls we discovered multiple times in code reviews over the years, as well a few patterns we identified as helpful. Nested subscriptions in use A very common use case is to request a data object from our API to then…

Read article
 | Thomas Hilzendegen

What are Promises? Fun Fact: Promises are a commitment for value over time; they promise this. But let me be more precise: There was a time in JavaScript before Promises. It was a dark age. Every developer was scared to end in hell – the callback hell. A callback is a function…

Read article