Observable
A representation of any set of values over any amount of time. This is the most basic building block of RxJS.
Subclasses
-
ConnectableObservable -
GroupedObservable -
Subject-
BehaviorSubject -
ReplaySubject -
AsyncSubject
-
Static Properties
| Property | Type | Description |
|---|---|---|
| create |
Creates a new cold Observable by calling the Observable constructor |
|
| if | ||
| throw |
Constructor
Properties
| Property | Type | Description |
|---|---|---|
| _isScalar |
Internal implementation detail, do not use directly. |
|
| source | ||
| operator |
Methods
lift() | ||
|---|---|---|
|
Creates a new Observable, with this Observable as the source, and the passed operator defined as the new observable's operator. |
||
Parameters
Returns
|
subscribe() | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Invokes an execution of an Observable and registers Observer handlers for notifications it will emit. |
||||||||||||||||||||||||||
|
| observer |
Optional. Default is Type: |
Returns
Subscription
Parameters
| next |
Type: |
| error |
Type: |
| complete |
Type: |
Returns
Subscription
Parameters
| next |
Type: |
| error |
Type: |
| complete |
Optional. Default is Type: |
Returns
Subscription
Parameters
| next |
Type: |
| error |
Type: |
| complete |
Type: |
Returns
Subscription
Parameters
| next |
Optional. Default is Type: |
| error |
Optional. Default is Type: |
| complete |
Optional. Default is Type: |
Returns
Subscription
Use it when you have all these Observables, but still nothing is happening.
subscribe is not a regular operator, but a method that calls Observable's internal subscribe function. It
might be for example a function that you passed to Observable's constructor, but most of the time it is
a library implementation, which defines what will be emitted by an Observable, and when it be will emitted. This means
that calling subscribe is actually the moment when Observable starts its work, not when it is created, as it is often
the thought.
Apart from starting the execution of an Observable, this method allows you to listen for values that an Observable emits, as well as for when it completes or errors. You can achieve this in two of the following ways.
The first way is creating an object that implements Observer interface. It should have methods
defined by that interface, but note that it should be just a regular JavaScript object, which you can create
yourself in any way you want (ES6 class, classic function constructor, object literal etc.). In particular do
not attempt to use any RxJS implementation details to create Observers - you don't need them. Remember also
that your object does not have to implement all methods. If you find yourself creating a method that doesn't
do anything, you can simply omit it. Note however, if the error method is not provided, all errors will
be left uncaught.
The second way is to give up on Observer object altogether and simply provide callback functions in place of its methods.
This means you can provide three functions as arguments to subscribe, where the first function is equivalent
of a next method, the second of an error method and the third of a complete method. Just as in case of Observer,
if you do not need to listen for something, you can omit a function, preferably by passing undefined or null,
since subscribe recognizes these functions by where they were placed in function call. When it comes
to error function, just as before, if not provided, errors emitted by an Observable will be thrown.
Whichever style of calling subscribe you use, in both cases it returns a Subscription object.
This object allows you to call unsubscribe on it, which in turn will stop the work that an Observable does and will clean
up all resources that an Observable used. Note that cancelling a subscription will not call complete callback
provided to subscribe function, which is reserved for a regular completion signal that comes from an Observable.
Remember that callbacks provided to subscribe are not guaranteed to be called asynchronously.
It is an Observable itself that decides when these functions will be called. For example of
by default emits all its values synchronously. Always check documentation for how given Observable
will behave when subscribed and if its default behavior can be modified with a scheduler.
Example
Subscribe with an Observer
Subscribe with functions
Cancel a subscription
_trySubscribe() | ||
|---|---|---|
Parameters
Returns
|
forEach() | ||||
|---|---|---|---|---|
Parameters
Returns
|
pipe() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Used to stitch together functional operators into a chain. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| op1 |
Type: |
Returns
Observable<A>
Parameters
| op1 |
Type: |
| op2 |
Returns
Observable<B>
Parameters
| op1 |
Type: |
| op2 | |
| op3 |
Type: |
Returns
Observable<C>
Parameters
| op1 |
Type: |
| op2 | |
| op3 |
Type: |
| op4 |
Type: |
Returns
Observable<D>
Parameters
| op1 |
Type: |
| op2 | |
| op3 |
Type: |
| op4 |
Type: |
| op5 |
Type: |
Returns
Observable<E>
Parameters
| op1 |
Type: |
| op2 | |
| op3 |
Type: |
| op4 |
Type: |
| op5 |
Type: |
| op6 |
Type: |
Returns
Observable<F>
Parameters
| op1 |
Type: |
| op2 | |
| op3 |
Type: |
| op4 |
Type: |
| op5 |
Type: |
| op6 |
Type: |
| op7 |
Type: |
Returns
Observable<G>
Parameters
| op1 |
Type: |
| op2 | |
| op3 |
Type: |
| op4 |
Type: |
| op5 |
Type: |
| op6 |
Type: |
| op7 |
Type: |
| op8 |
Type: |
Returns
Observable<H>
Parameters
| op1 |
Type: |
| op2 | |
| op3 |
Type: |
| op4 |
Type: |
| op5 |
Type: |
| op6 |
Type: |
| op7 |
Type: |
| op8 |
Type: |
| op9 |
Type: |
Returns
Observable<I>
Parameters
| op1 |
Type: |
| op2 | |
| op3 |
Type: |
| op4 |
Type: |
| op5 |
Type: |
| op6 |
Type: |
| op7 |
Type: |
| op8 |
Type: |
| op9 |
Type: |
| operations |
Type: |
Returns
Observable<{ }>
toPromise() | ||||
|---|---|---|---|---|
|
| PromiseCtor |
Type: |
Returns
Promise<T>
Parameters
| PromiseCtor |
Type: |
Returns
Promise<T>