An Observable is the core building block of RxJS. It represents a lazy Push collection of multiple values that can be delivered synchronously or asynchronously over time.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ReactiveX/rxjs/llms.txt
Use this file to discover all available pages before exploring further.
What is an Observable?
Observables fill a unique role in JavaScript’s data production paradigm:| Single | Multiple | |
|---|---|---|
| Pull | Function | Iterator |
| Push | Promise | Observable |
Key Insight: An Observable is like a function that can return multiple values over time, both synchronously and asynchronously.
Pull vs Push Systems
Pull Systems: The consumer decides when to receive data from the producer.- Functions: You call them to get a single value
- Iterators: You call
next()to get multiple values
- Promises: Deliver one value when ready
- Observables: Deliver zero to infinite values when ready
Type Signature
Creating Observables
Using the Constructor
The Observable constructor takes a subscribe function that defines how values are produced:Using Creation Operators
Subscribing to Observables
- With Observer Object
- With Next Function
- Partial Observer
Observable Execution
Notification Types
An Observable execution can deliver three types of notifications:- Next: Sends a value (Number, String, Object, etc.)
- Error: Sends a JavaScript Error (terminates the execution)
- Complete: Sends no value (terminates the execution)
Observable Contract
All Observable executions follow this pattern (expressed as a regular expression):- Zero to infinite Next notifications
- Optionally followed by either one Error OR one Complete
- Nothing can be delivered after Error or Complete
Synchronous vs Asynchronous
Observables can deliver values either synchronously or asynchronously - they are not inherently async.
Disposing Observable Executions
Subscribing returns aSubscription object that can be used to cancel the execution:
Cleanup Function
When creating an Observable, return a cleanup function to release resources:Observables vs Functions
Observables are like functions with superpowers:- Function
- Observable
Both functions and Observables are lazy - they don’t execute until called/subscribed.
Observables vs Promises
| Feature | Promise | Observable |
|---|---|---|
| Values | Single value | Multiple values |
| Lazy | No (eager) | Yes (lazy) |
| Cancellable | No | Yes (via unsubscribe) |
| Operators | Limited (then, catch) | Extensive library |
| Multicast | Yes | No (unless using Subject) |
Common Patterns
Error Handling
Finite Streams
Infinite Streams
Best Practices
Naming Convention: Suffix Observable variable names with
$ (e.g., data$, clicks$) to distinguish them from regular values.- Always handle errors - Unhandled errors can crash your application
- Unsubscribe when done - Prevent memory leaks in long-lived components
- Use creation operators - Prefer
of,from, etc. overnew Observable - Keep subscribe logic minimal - Use operators for transformations
- Return cleanup functions - Always clean up resources like timers and event listeners
When to Use Observables
Use Observables when you need:- Multiple values over time
- Cancellation support
- Lazy execution
- Powerful composition via operators
- Complex async coordination
- User input events
- WebSocket messages
- HTTP polling
- Real-time data streams
- Animation sequences
Related Concepts
- Observer - Consumes values from Observables
- Subscription - Manages Observable execution lifecycle
- Operators - Transform and compose Observables
- Subject - Special Observable that multicasts to multiple Observers
