-
Notifications
You must be signed in to change notification settings - Fork 1
/
forkjoin.js
57 lines (48 loc) · 2.01 KB
/
forkjoin.js
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
/*
Not only does forkJoin require all input observables to be completed, but it also returns an observable
that produces a single value that is an array of the last values produced by the input observables.
In other words, it waits until the last input observable completes, and then produces a single value and completes.
In contrast, combineLatest returns an Observable that produces a new value every time the input observables do,
once all input observables have produced at least one value. This means it could have infinite values and may not
complete. It also means that the input observables don't have to complete before producing a value.
*/
const of = require('rxjs/observable/of').of;
const flatMap = require('rxjs/operators').flatMap;
const Observable = require('rxjs/observable').Observable;
const forkJoin = require('rxjs').forkJoin;
const combineLatest = require('rxjs').combineLatest;
const observable = new Observable(subscriber => {
subscriber.next('First data emitted');
subscriber.next('Second data emitted');
console.log('Waiting 3 seconds to complete');
setTimeout(() => {
subscriber.complete();
}, 3000);
});
console.log('Starting ForkJoin');
of('ForkJoin')
.pipe(
flatMap(x => forkJoin([of(x), observable]))
)
.subscribe(_ => {
console.log('ForkJoin done', _);
// ============== CombineLatest ===============
console.log('Starting CombineLatest');
of('CombineLatest')
.pipe(
flatMap(x => combineLatest([of(x), observable]))
)
.subscribe(_ => {
console.log('CombineLatest done', _);
});
// ============== end ===============
});
/*
Starting ForkJoin
Waiting 3 seconds to complete
ForkJoin done [ 'ForkJoin', 'Second data emitted' ]
Starting CombineLatest
CombineLatest done [ 'CombineLatest', 'First data emitted' ]
CombineLatest done [ 'CombineLatest', 'Second data emitted' ]
Waiting 3 seconds to complete
*/