Whoa
Whoa
Pros:
Cons:
In RxJS, you can use the `map` operator to transform data emitted by an observable. It's like a
powerhouse for data transformation.
```javascript
The `filter` operator lets you selectively emit values based on a condition.
```javascript
```
```javascript
);
source.subscribe(
);
```
`combineLatest` merges the latest values from multiple observables into an array, producing a new value
whenever any of the source observables emits. It's like a synchronizing dance where partners
(observables) step together whenever any one of them takes a step.
```javascript
});
```
- **Subject:** A special type of observable that allows values to be multicasted to multiple observers.
Think of it as a radio station. When a song (value) is played, all listeners (observers) hear it.
```javascript
subject.next('Hello');
subject.next('World');
```
- **BehaviorSubject:** Extends `Subject` and requires an initial value. It remembers the latest value and
emits it to new subscribers immediately upon subscription.
```javascript
behaviorSubject.next('Hello');
behaviorSubject.next('World');
```
Observables vs Promises - what's the difference?
- **Observables:**
- Support cancellation.
- **Promises:**
- Always asynchronous.
- **Cold Observables:** Produce values independently for each subscriber. Each subscriber gets its own
set of values.
```javascript
```
- **Hot Observables:** Share the same set of values among all subscribers. Subscribers might miss
values if they subscribe late.
```javascript
```
And now, for the grand finale, `concatMap`, `switchMap`, `mergeMap`, `map`, and `exhaustMap` in RxJS.
`concatMap`: Maps each value to an observable and concatenates them, maintaining the order.
`switchMap`: Maps each value to an observable, cancels the previous observable, and switches to the
new one.
`mergeMap`: Maps each value to an observable and merges the resulting observables concurrently.
`exhaustMap`: Maps each value to an observable but ignores new values until the inner observable
completes.