Create an observable emitting each item passed in.
Create an observable from a Promise or Iterable.
Emit whatever and whenever the passed observables emit.
Emit with the value for each observable in its corresponding position in an array.
Wait for all passed observable to emit once — i.e. array won’t have any holes with missing values.
A constant observable that does not emit and immediately completes. Sometimes useful for composing with operators.
Transform from one value to another value.
Your function: (A) => B
Observable: Observable<A> --> Observable<B>
Ignore events you don’t want.
Your function: (A) => boolean
Observable: Observable<A> --> Observable<A>
Combine two adjacent events into a summarised value. e.g. sum a total
Your function: (B, A) => B
Observable: Observable<A> --> Observable<B>
Transform from a value to a brand new observable.
Your function: (A) => Observable<B>
Observable: Observable<A> --> Observable<B>
Recover an error into a brand new observable.
Your function: (Error) => Observable<B>
Observable: Observable<A> --> Observable<B>
Ensures the observable completes after n
number of values have been emitted.
Fail or recover if the observable doesn’t emit within a time period.
- New subscribers will share the same underlying source observable (instead of say making a new HTTP request for each subscriber)
- by caching the last emitted value (
bufferSize: 1
) - and will tear down that source observable when there are no more subscribers left (
refCount: true
)
Allow you to perform side effects using the emitted events, such as logging or updating some external state. Best to use this sparingly.
const promise = fetch("/api");
promise.then(() => console.log("loaded"));
promise.then(() => console.log("loaded"));
// How many HTTP requests are made?
const o$ = httpClient.get("/api");
o$.subscribe(() => console.log("loaded"));
o$.subscribe(() => console.log("loaded"));
// How many HTTP requests are made?
- A promise either resolves (happy path), or rejects (sad path). It can’t do both.
- An observable emit zero, one, or many times (happy path).
- An observable may complete, which means it stop emitting events and all subscribers are unsubscribed.
- An observable may error (sad path), which means it also completes and stops emitting events.
- A promise represents some future value. If you have a promise, then it has already started fetching, loading, etc.
- An observable represents many futures values. If have an observable, then it might have started fetching/loading/listening or it might not have.
You chain observables together to declare the conveyor belt for how values will flow in your application.
They flow starting from external sources (user interactions, route params, timers) through external systems (HTTP communication with APIs, local caches) and back out.
The final result of your chain will often be some sort of value to present to a user (e.g. a view model of the current state from the API).
You can think of declaring a chain of observable as being similar to declaring a class. A class only becomes alive when you instatiate an instance of it — an object. And you can instantiate multiple objects of that single class you defined.
Similarly, a chain of observables usually only becomes alive when you subscribe to it. And a single chain can have one or many subscribers.
combineLatest
with circular reference to inputs. Instead use a closure with a nested.pipe
.- Defensive
shareReplay
everywhere. BehaviorSubject
everywhere. Don’t treat subjects as mutable variables.tap
that does a lot of work.
- Observables model when as well as what. If two pieces of state change together, perhaps they should be the same observable or same subject?