take

signature: take(count: number): Observable

Emit provided number of values before completing.


:bulb: 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

( jsBin | jsFiddle )

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

( jsBin | jsFiddle )

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


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/take.ts

results matching ""

    No results matching ""