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

first

PreviousfindNextignoreElements

Last updated 5 years ago

Was this helpful?

signature: first(predicate: function, select: function)

단순히 첫번째 값, 혹은 조건에 맞는 첫번째 값을 발생시킵니다

first의 정반대는 입니다!

만약 다음 통지가 전송되기 전에 옵저버블이 완료된다면, First 는 EmptyError를 옵저버의 콜백 에러에 전달합니다.이러한 동작을 원하지 않으면, take(1) 을 대신 사용하세요.

예시

( )

예시 1: 연속된 값들 중 첫번째 값

( | | )

// RxJS v6+
import { from } from 'rxjs';
import { first } from 'rxjs/operators';

const source = from([1, 2, 3, 4, 5]);
//arguments가 없으면, 첫번째 값을 발생시킵니다
const example = source.pipe(first());
//결과: "First value: 1"
const subscribe = example.subscribe(val => console.log(`First value: ${val}`));

예시 2: 조건을 만족하는 첫번째 값

// RxJS v6+
import { from } from 'rxjs';
import { first } from 'rxjs/operators';

const source = from([1, 2, 3, 4, 5]);
//조건을 만족하는 첫번째 값을 발생시킵니다
const example = source.pipe(first(num => num === 5));
//결과: "First to pass test: 5"
const subscribe = example.subscribe(val =>
  console.log(`First to pass test: ${val}`)
);

예시 3: 기본값 사용

// RxJS v6+
import { from } from 'rxjs';
import { first } from 'rxjs/operators';

const source = from([1, 2, 3, 4, 5]);
//아무 값도 조건을 통과하지 못했으므로, 기본값을 발생시킵니다
const example = source.pipe(first(val => val > 5, 'Nothing'));
//결과: 'Nothing'
const subscribe = example.subscribe(val => console.log(val));

추가 자료

( | | )

( | | )

- 공식 문서

- André Staltz

Source Code:

💡
💡
last
테스트 예시
StackBlitz
jsBin
jsFiddle
StackBlitz
jsBin
jsFiddle
StackBlitz
jsBin
jsFiddle
📰
first
📹
💵
Filtering operator: take, first, skip
📂
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/first.ts