signature: takeWhile(predicate: function(value, index): boolean, inclusive?: boolean): Observable
넘겨받은 표현식이 참인 동안, 값을 발생시킵니다.
// RxJS v6+
import { of } from 'rxjs';
import { takeWhile } from 'rxjs/operators';
// 1,2,3,4,5를 발생시킵니다
const source$ = of(1, 2, 3, 4, 5);
//4 이하인 값만 발생시키고, 종료합니다
source$
.pipe(takeWhile(val => val <= 4))
// log: 1,2,3,4
.subscribe(val => console.log(val));
// RxJS v6.4+
import { of } from 'rxjs';
import { takeWhile, filter } from 'rxjs/operators';
const source$ = of(1, 2, 3, 9);
source$
// inclusive 를 설정하면, false를 리턴하게 하는 값도 발생되어집니다
.pipe(takeWhile(val => val <= 3, true))
// log: 1, 2, 3, 9
.subscribe(console.log);
// RxJS v6+
import { of } from 'rxjs';
import { takeWhile, filter } from 'rxjs/operators';
// 3, 3, 3, 9, 1, 4, 5, 8, 96, 3, 66, 3, 3, 3를 발생시킵니다
const source$ = of(3, 3, 3, 9, 1, 4, 5, 8, 96, 3, 66, 3, 3, 3);
// 소스의 값이 3일 때만 값을 발생시키고, 종료합니다
source$
.pipe(takeWhile(it => it === 3))
// log: 3, 3, 3
.subscribe(val => console.log('takeWhile', val));
source$
.pipe(filter(it => it === 3))
// log: 3, 3, 3, 3, 3, 3, 3
.subscribe(val => console.log('filter', val));