RxJS 배우기
  • 소개
  • RxJS 배우기
    • 연산자
      • Combination
        • combineAll
        • combineLatest
        • concat
        • concatAll
        • endWith
        • forkJoin
        • merge
        • mergeAll
        • pairwise
        • race
        • startWith
        • withLatestFrom
        • zip
      • Conditional
        • defaultIfEmpty
        • every
        • iif
        • sequenceEqual
      • Creation
        • ajax
        • create
        • defer
        • empty
        • from
        • fromEvent
        • generate
        • interval
        • of
        • range
        • throw
        • timer
      • Error Handling
        • catch / catchError
        • retry
        • retryWhen
      • Multicasting
        • publish
        • multicast
        • share
        • shareReplay
      • Filtering
        • audit
        • auditTime
        • debounce
        • debounceTime
        • distinct
        • distinctUntilChanged
        • distinctUntilKeyChanged
        • filter
        • find
        • first
        • ignoreElements
        • last
        • sample
        • single
        • skip
        • skipUntil
        • skipWhile
        • take
        • takeLast
        • takeUntil
        • takeWhile
        • throttle
        • throttleTime
      • Transformation
        • buffer
        • bufferCount
        • bufferTime
        • bufferToggle
        • bufferWhen
        • concatMap
        • concatMapTo
        • exhaustMap
        • expand
        • groupBy
        • map
        • mapTo
        • mergeMap / flatMap
        • mergeScan
        • partition
        • pluck
        • reduce
        • scan
        • switchMap
        • switchMapTo
        • toArray
        • window
        • windowCount
        • windowTime
        • windowToggle
        • windowWhen
      • Utility
        • tap / do
        • delay
        • delayWhen
        • dematerialize
        • finalize / finally
        • let
        • repeat
        • timeInterval
        • timeout
        • timeoutWith
        • toPromise
      • 전체 목록
    • Subjects
      • AsyncSubject
      • BehaviorSubject
      • ReplaySubject
      • Subject
    • 사용예시
      • Alphabet Invasion Game
      • Battleship Game
      • Breakout Game
      • Car Racing Game
      • Catch The Dot Game
      • Click Ninja Game
      • Flappy Bird Game
      • Game Loop
      • Horizontal Scroll Indicator
      • Http Polling
      • Lockscreen
      • Matrix Digital Rain
      • Memory Game
      • Mine Sweeper Game
      • Platform Jumper Game
      • Progress Bar
      • Save Indicator
      • Smart Counter
      • Space Invaders Game
      • Stop Watch
      • Swipe To Refresh
      • Tank Battle Game
      • Tetris Game
      • Type Ahead
      • Uncover Image Game
    • 개념
      • RxJS 입문서
      • RxJS v5 -> v6 업그레이드
      • 시간 기반의 연산자 비교
      • 연산자 imports의 이해
Powered by GitBook
On this page
  • 넘겨받은 표현식이 참인 동안, 값을 발생시킵니다.
  • 예시
  • 관련 사용법
  • 추가 자료

Was this helpful?

  1. RxJS 배우기
  2. 연산자
  3. Filtering

takeWhile

PrevioustakeUntilNextthrottle

Last updated 5 years ago

Was this helpful?

signature: takeWhile(predicate: function(value, index): boolean, inclusive?: boolean): Observable

넘겨받은 표현식이 참인 동안, 값을 발생시킵니다.

옵션 파라미터인 inclusive 가 true 로 지정되면, 참을 만족하지못하는 첫번째 아이템 또한 발생시킵니다.

예시

예시 1: 조건에 해당하는 값만 받습니다

( | | )

// 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));

예시2: (v6.4+) inclusive를 활용한 takeWhile

// 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);

예시 3: takeWhile 과 filter의 차이점

// 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));

관련 사용법

추가 자료

( )

( | | )

- 공식 문서

- John Linquist

Source Code:

💡
StackBlitz
jsBin
jsFiddle
StackBlitz
StackBlitz
jsBin
jsFiddle
Alphabet Invasion Game
Battleship Game
Breakout Game
Car Racing Game
Catch The Dot Game
Click Ninja Game
Flappy Bird Game
Mine Sweeper Game
Platform Jumper Game
Smart Counter
Swipe To Refresh
Tetris Game
Uncover Image Game
📰
takeWhile
📹
💵
Completing a stream with takeWhile
📂
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/takeWhile.ts