-
Notifications
You must be signed in to change notification settings - Fork 827
/
examples-cancellable-with-takeUntil.ts
29 lines (21 loc) · 938 Bytes
/
examples-cancellable-with-takeUntil.ts
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
import { Observable } from "rxjs"
import { interval, timer } from "rxjs"
import { takeUntil } from "rxjs/operators"
/* An observable is Cancellable. Here we will implement it with takeUntil() rxjs operator
The TakeUntil subscribes and begins mirroring the source Observable. It also monitors a second Observable that you provide. If this second Observable emits an item or sends a termination notification, the Observable returned by TakeUntil stops mirroring the source Observable and terminates.
*/
let a = new Observable((observer) => {
setTimeout(() => observer.next(1), 1000)
setTimeout(() => observer.next(2), 2000)
setTimeout(() => observer.next(3), 3000)
setTimeout(() => observer.complete(), 4000)
})
let b = new Observable((observer) => {
setTimeout(() => observer.next(), 2500)
setTimeout(() => observer.complete(), 4000)
})
a.pipe(takeUntil(b)).subscribe((result) => console.log(result))
/* Output
1
2
*/