first

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

Emit the first value or first to pass provided expression.


💡 The counterpart to first is last!

💡 First will deliver an EmptyError to the Observer's error callback if the Observable completes before any next notification was sent. If you don't want this behavior, use take(1) instead.


Ultimate RxJS

Examples

( example tests )

Example 1: First value from sequence

( StackBlitz | jsBin | jsFiddle )

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

const source = from([1, 2, 3, 4, 5]);
//no arguments, emit first value
const example = source.pipe(first());
//output: "First value: 1"
const subscribe = example.subscribe(val => console.log(`First value: ${val}`));

Example 2: First value to pass predicate

( StackBlitz | jsBin | jsFiddle )

Example 3: Utilizing default value

( StackBlitz | jsBin | jsFiddle )

Additional Resources


📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/first.ts

Last updated