Observable: Angular's Push-Based Data Stream
An Observable is a lazy push collection that emits values over time. Angular uses it for HTTP and events, letting components react instead of polling. Forgetting to unsubscribe leaks memory and keeps destroyed components alive.
WHY IT EXISTS: Before Observables, async code in browsers was a tangle of callbacks. You registered a handler and hoped you remembered to remove it. RxJS Observables treat events, HTTP responses, and user input as composable streams with a uniform API, giving you a single abstraction for all async sources and a clear way to start and stop listening. They turn scattered async logic into something you can map, filter, and merge like arrays.
THE MENTAL MODEL: Think of an Observable as a lazy newsletter. Nothing happens until someone subscribes. Once they do, the publisher starts sending issues. The subscriber can unsubscribe at any time, and the stream stops for them. It is push-based, meaning the producer decides when the next value arrives, not the consumer. This inversion of control separates the data source from the code that reacts to it.
HOW IT WORKS: An Observable is fundamentally a function that accepts an Observer and returns a teardown function. When you call subscribe, that function executes. It can emit next values, errors, or a completion signal. In Angular, HttpClient.get returns an Observable that wraps the underlying XMLHttpRequest. The request does not fire until subscribe is called. You chain operators like map, filter, or switchMap to transform the stream declaratively, keeping your component code flat and readable.
WHEN TO USE IT: Use Observables when you expect multiple values over time, need to cancel ongoing work, or want to compose async operations cleanly. Angular's HttpClient uses Observables even for single responses because they can be retried, debounced, and combined with other streams using operators like combineLatest. They shine in autocomplete boxes where keystrokes must be debounced and previous in-flight searches cancelled automatically.
WHEN NOT TO USE IT: Do not wrap simple one-shot values in an Observable if you never need to cancel, retry, or combine them with other streams. A plain Promise or async/await is often clearer for a single fire-and-forget task. Also avoid nesting subscriptions inside subscriptions, a pattern called the pyramid of doom. Instead, flatten inner Observables with mergeMap or switchMap to maintain a single subscription tree.
ONE CANONICAL EXAMPLE: Imagine a search component that calls http.get on every keystroke. Without Observables, you would manually manage timers, guard against race conditions, and abort stale requests. With RxJS, you pipe the input event Observable through debounceTime to wait for a pause, distinctUntilChanged to skip duplicates, and switchMap to the latest HTTP call. If the user types quickly, pending requests are cancelled automatically, and only the freshest result reaches the UI in about five lines of declarative code.
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.