single

signature: single(a: Function): Observable

Emit single item that passes expression.

Ultimate RxJS

Examples

Example 1: Emit first number passing predicate

( StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { from } from 'rxjs';
import { single } from 'rxjs/operators';
//emit (1,2,3,4,5)
const source = from([1, 2, 3, 4, 5]);
//emit one item that matches predicate
const example = source.pipe(single(val => val === 4));
//output: 4
const subscribe = example.subscribe(val => console.log(val));

Additional Resources

  • single 📰 - Official docs

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