Observable S1
- Stage: Stage 1
- Status: Active
- ECMAScript edition: —
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal introduces an Observable type to the ECMAScript standard library to model push-based data sources like DOM events, timers, and sockets. It is compositional and lazy, allowing observables to be combined and only emit after subscription. The proposal defines the core Observable interface, subscribe method, and static methods of/from, along with Observer and SubscriptionObserver types, with a note that combinators like filter and map are not yet included.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
ECMAScript Observable
This proposal introduces an Observable type to the ECMAScript standard library. The Observable type can be used to model push-based data sources such as DOM events, timer intervals, and sockets. In addition, observables are:
- Compositional: Observables can be composed with higher-order combinators.
- Lazy: Observables do not start emitting data until an observer has subscribed.
Example: Observing Keyboard Events
Using the Observable constructor, we can create a function which returns an observable stream of events for an arbitrary DOM element and event type.
We can then use standard combinators to filter and map the events in the stream, just like we would with an array.
Note: The "filter" and "map" methods are not included in this proposal. They may be added in a future version of this specification.
When we want to consume the event stream, we subscribe with an observer.
The object returned by subscribe will allow us to cancel the subscription at any time. Upon cancelation, the Observable's cleanup function will be executed.
Motivation
The Observable type represents one of the fundamental protocols for processing asynchronous streams of data. It is particularly effective at modeling streams of data which originate from the environment and are pushed into the application, such as user interface events. By offering Observable as a component of the ECMAScript standard library, we allow platforms and applications to share a common push-based stream protocol.
Implementations
Running Tests
To run the unit tests, install the es-observable-tests package into your project.
Then call the exported runTests function with the constructor you want to test.
API
Observable
An Observable represents a sequence of values which may be observed.
Observable.of
Observable.of creates an Observable of the values provided as arguments. The values
are delivered synchronously when subscribe is called.
Observable.from
Observable.from converts its argument to an Observable.
- If the argument has a
Symbol.observablemethod, then it returns the result of invoking that method. If the resulting object is not an instance of Observable, then it is wrapped in an Observable which will delegate subscription. - Otherwise, the argument is assumed to be an iterable and the iteration values are
delivered synchronously when
subscribeis called.
Converting from an object which supports Symbol.observable to an Observable:
Converting from an iterable to an Observable:
Observer
An Observer is used to receive data from an Observable, and is supplied as an argument to subscribe.
All methods are optional.
SubscriptionObserver
A SubscriptionObserver is a normalized Observer which wraps the observer object supplied to subscribe.