// RxJS v6+import { interval, timer } from'rxjs';import { takeUntil } from'rxjs/operators';//매 1초마다 값을 발생시킵니다constsource=interval(1000);//5초 뒤, 값을 발생시킵니다consttimer$=timer(5000);//타이머가 5초 뒤 값을 발생시키면, 완료시킵니다constexample=source.pipe(takeUntil(timer$));//결과: 0,1,2,3constsubscribe=example.subscribe(val =>console.log(val));
// RxJS v6+import { interval } from'rxjs/observable/interval';import { takeUntil, filter, scan, map, withLatestFrom } from'rxjs/operators';//매 1초마다 값을 발생시킵니다constsource=interval(1000);//짝수 체크constisEven= val => val %2===0;//짝수인 값만 받습니다constevenSource=source.pipe(filter(isEven));//짝수의 합을 구합니다constevenNumberCount=evenSource.pipe(scan((acc, _) => acc +1,0));//5개의 짝수가 값을 발생시키기 전까진, 값을 발생시키지 않습니다constfiveEvenNumbers=evenNumberCount.pipe(filter(val => val >5));constexample=evenSource.pipe(//현재 짝수의 갯수를 출력합니다withLatestFrom(evenNumberCount),map(([val, count]) =>`Even number (${count}) : ${val}`),//5개의 짝수가 발생되어지면, 옵저버블을 종료시킵니다takeUntil(fiveEvenNumbers));/* Even number (1) : 0, Even number (2) : 2 Even number (3) : 4 Even number (4) : 6 Even number (5) : 8*/constsubscribe=example.subscribe(val =>console.log(val));