-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathflatmap.js
More file actions
61 lines (42 loc) · 1.63 KB
/
flatmap.js
File metadata and controls
61 lines (42 loc) · 1.63 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
let {Observable} = require('rxjs/Observable');
require('rxjs/add/observable/from');
require('rxjs/add/operator/mergeMap'); // flatMap is an alias of mergeMap
function getDrinks() {
let beers = Observable.from([
{name: "Stella", country: "Belgium", price: 9.50},
{name: "Sam Adams", country: "USA", price: 8.50},
{name: "Bud Light", country: "USA", price: 6.50}
]);
let softDrinks = Observable.from([
{name: "Coca Cola", country: "USA", price: 1.50},
{name: "Fanta", country: "USA", price: 1.50},
{name: "Lemonade", country: "France", price: 2.50}
]);
return Observable.create( observer => {
observer.next(beers); // pushing the beer pallet (observable)
observer.next(softDrinks); // pushing the soft drinks pallet (observable)
observer.complete();
}
);
}
// We want to unload each pallet and print the into about each case with drinks
getDrinks()
.flatMap(drinks => drinks) // unloading drinks from pallets
.subscribe(
drink => console.log("Subscriber got " + drink.name + ": " + drink.price ),
error => console.err(error),
() => console.log("The stream of observables is over")
);
/*
// AN alternative (bad) solution with nested subscribtions
getDrinks()
.subscribe( pallet =>
pallet.subscribe(
drink => console.log("Nested subscriber got " + drink.name + ": " + drink.price ),
error => console.err(error),
() => console.log("The stream of nested observables is over"))
,
error => console.err(error),
() => console.log("The stream of source observables is over")
);
*/