take
연산자(operator) 정의: take(count: number): Observable
Emit provided number of values before completing.
If you want to take a variable number of values based on some logic, or another observable, you can use takeUntil or takeWhile!
Examples
Example 1: Take 1 value from source
//emit 1,2,3,4,5
const source = Rx.Observable.of(1,2,3,4,5);
//take the first emitted value then complete
const example = source.take(1);
//output: 1
const subscribe = example.subscribe(val => console.log(val));
Example 2: Take the first 5 values from source
//emit value every 1s
const interval = Rx.Observable.interval(1000);
//take the first 5 emitted values
const example = interval.take(5);
//output: 0,1,2,3,4
const subscribe = example.subscribe(val => console.log(val));
Additional Resources
- take - Official docs
- Filtering operator: take, first, skip - André Staltz
Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/take.ts