function stable operator
Emits only the first value (or the first value that meets some condition) emitted by the source Observable.
first<T, D>(predicate?: (value: T, index: number, source: Observable<T>) => boolean, defaultValue?: D): OperatorFunction<T, T | D> predicate | (value: T, index: number, source: Observable<T>) => boolean | Optional. Default is An optional function called with each item to test for condition matching. |
defaultValue | D | Optional. Default is The default value emitted in case no valid value was found on the source. |
OperatorFunction<T, T | D>: A function that returns an Observable that emits the first item that matches the condition.
EmptyError Delivers an EmptyError to the Observer's error callback if the Observable completes before any next notification was sent. This is how first() is different from take(1) which completes instead.
Emits only the first value. Or emits only the first value that passes some test.
If called with no arguments, first emits the first value of the source Observable, then completes. If called with a predicate function, first emits the first value of the source that matches the specified condition. Emits an error notification if defaultValue was not provided and a matching element is not found.
Emit only the first click that happens on the DOM
import { fromEvent, first } from 'rxjs';
const clicks = fromEvent(document, 'click');
const result = clicks.pipe(first());
result.subscribe(x => console.log(x)); Emits the first click that happens on a DIV
import { fromEvent, first } from 'rxjs';
const div = document.createElement('div');
div.style.cssText = 'width: 200px; height: 200px; background: #09c;';
document.body.appendChild(div);
const clicks = fromEvent(document, 'click');
const result = clicks.pipe(first(ev => (<HTMLElement>ev.target).tagName === 'DIV'));
result.subscribe(x => console.log(x)); first(predicate?: null, defaultValue?: D): OperatorFunction<T, T | D> predicate | null | Optional. Default is |
defaultValue | D | Optional. Default is |
OperatorFunction<T, T | D>
first(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>> predicate | BooleanConstructor |
OperatorFunction<T, TruthyTypesOf<T>>
first(predicate: BooleanConstructor, defaultValue: D): OperatorFunction<T, TruthyTypesOf<T> | D> predicate | BooleanConstructor | |
defaultValue | D |
OperatorFunction<T, TruthyTypesOf<T> | D>
first(predicate: (value: T, index: number, source: Observable<T>) => value is S, defaultValue?: S): OperatorFunction<T, S> predicate | (value: T, index: number, source: Observable<T>) => value is S | |
defaultValue | S | Optional. Default is |
OperatorFunction<T, S>
first(predicate: (value: T, index: number, source: Observable<T>) => value is S, defaultValue: D): OperatorFunction<T, S | D> predicate | (value: T, index: number, source: Observable<T>) => value is S | |
defaultValue | D |
OperatorFunction<T, S | D>
first(predicate: (value: T, index: number, source: Observable<T>) => boolean, defaultValue?: D): OperatorFunction<T, T | D> predicate | (value: T, index: number, source: Observable<T>) => boolean | |
defaultValue | D | Optional. Default is |
OperatorFunction<T, T | D>
© 2015–2022 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/first