liveBook · Manning

Subscription

As explained in The Joy of JavaScript

A Subscription is an object that represents an ongoing connection to a stream or observable source for receiving values over time.

A Subscription represents an ongoing connection to a stream or Observable, allowing emitted values to reach a subscriber over time. Subscriptions make the push model explicit: subscribing starts a lazy Observable, while unsubscribing ends the connection. section 9.3.1 section 9.4.1

Its minimal interface contains one unsubscribe method. The implementation supplies the cleanup appropriate to the producer, such as clearing a setInterval that emits values intermittently. section 9.4.1

Subscribe, then unsubscribe

const obs$ = Observable.of('The', 'Joy', 'of', 'JavaScript'); const subs = obs$.subscribe({ next: console.log }); subs.unsubscribe(); The returned object separates receiving events from ending the stream connection.

How The Joy of JavaScript covers it

How deep it goes
Defines subscription behavior and cleanup, with examples, but defers detailed operator and library implementations.
Examples
Uses Observable.of with console logging, explicit unsubscribe, and setInterval cleanup.
What it assumes
Assumes familiarity with JavaScript values over time, while introducing streams, push delivery, Observables, and Observers together.
What you can do after
Operate an Observable connection by subscribing to events and invoking unsubscribe to stop delivery and perform cleanup.
How it connects
Connects streams, Observables, Observers, iterators, promises, async generators, and operators.

Also explained in

Compare how 2 books explain Subscription →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.