Canceling Sequences

In RxJS we can cancel a running Observable. This is an advantage over other asynchronous forms of communication, such as callbacks and promises, which can’t be directly canceled once they’re called (some promise implementations support cancellation, though).

There are two main ways we can cancel an Observable: implicitly and explicitly.

Explicit Cancellation: The Disposable

Observables themselves don’t have a method to get canceled. Instead, whenever we subscribe to an Observable we get a Disposable object that represents that particular subscription. We can then call the method dispose in that object, and that subscription will stop receiving notifications from the Observable.

In the following example, we subscribe ...

Get Reactive Programming with RxJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.