-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathflatMap_vs_switchMap.js
More file actions
25 lines (21 loc) · 899 Bytes
/
flatMap_vs_switchMap.js
File metadata and controls
25 lines (21 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
let {Observable} = require('rxjs/Observable');
require('rxjs/add/observable/interval');
require('rxjs/add/operator/mergeMap'); // flatMap is an alias of mergeMap
require('rxjs/add/operator/switchMap');
require('rxjs/add/operator/take');
require('rxjs/add/operator/map');
let outer = Observable.interval(1000).take(2);
let combined = outer.flatMap((x) => {
return Observable
.interval(400)
.take(3)
.map(y => `outer ${x}: inner ${y}`)
});
combined.subscribe(result => console.log(`result ${result} `));
/*
* Although the third value of the inner observable is emmited after the outer observable emits new value,
* all emissions of the inner observable are present in the merged output.
*
* Change flatMap to switchMap and the third value of the inner observable is not emitted because
* the outer observable emitted new value and the inner one starts handling it.
* */