Member-only story

RxJS: Composing Subscriptions

Nicholas Jamieson
4 min readFeb 16, 2018

--

Photo by Bradley Davis on Flickr

RxJS code involves making subscriptions to observables. Lots of subscriptions.

If each subscription is assigned to its own variable or property, the situation can be difficult to manage.

Fortunately, there are techniques — such as using takeUntil or takeWhile — that make dealing with subscriptions much easier. If you’re not familiar with the techniques, you should read Don’t Unsubscribe.

However, there are situations in which you still need to deal with subscriptions. For example, it’s sometimes necessary to manage subscriptions when writing operators.

Let’s look at subscriptions and at how subscription composition can be used to simplify some user-land operators.

What is a subscription?

The Subscription class and its related types look like this:

A Subscription instance is what’s returned from a call to subscribe and, most of the time, it’s only the subscription’s unsubscribe method that’s called. However, the Subscription class also has add and remove methods.

--

--

Nicholas Jamieson
Nicholas Jamieson

Written by Nicholas Jamieson

RxJS core team member; front-end developer; mentor; speaker; open-source contributor

Responses (3)