UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

43 lines (32 loc) 1.6 kB
# Subscription **What is a Subscription?** A Subscription is an object that represents a disposable resource, usually the execution of an Observable. A Subscription has one important method, `unsubscribe`, that takes no argument and just disposes the resource held by the subscription. In previous versions of RxJS, Subscription was called "Disposable". ```js var observable = Rx.Observable.interval(1000); var subscription = observable.subscribe(x => console.log(x)); // Later: // This cancels the ongoing Observable execution which // was started by calling subscribe with an Observer. subscription.unsubscribe(); ``` <span class="informal">A Subscription essentially just has an `unsubscribe()` function to release resources or cancel Observable executions.</span> Subscriptions can also be put together, so that a call to an `unsubscribe()` of one Subscription may unsubscribe multiple Subscriptions. You can do this by "adding" one subscription into another: ```js var observable1 = Rx.Observable.interval(400); var observable2 = Rx.Observable.interval(300); var subscription = observable1.subscribe(x => console.log('first: ' + x)); var childSubscription = observable2.subscribe(x => console.log('second: ' + x)); subscription.add(childSubscription); setTimeout(() => { // Unsubscribes BOTH subscription and childSubscription subscription.unsubscribe(); }, 1000); ``` When executed, we see in the console: ```none second: 0 first: 0 second: 1 first: 1 second: 2 ``` Subscriptions also have a `remove(otherSubscription)` method, in order to undo the addition of a child Subscription.