skip to content

single

 

Returns an Observable that emits a value if and only if the source observable emits a single value. No more, no less. Otherwise throws an error. Also takes an optional predicate to pre-filter values:

Also check out this first vs take vs single head-to-head comparison


No predicate — multiple values from the source will result in an error:

1
2
3
4
5
6
7
8
9
10
const { rxObserver } = require('api/v0.3');
const { timer } = require('rxjs');
const { single, take } = require('rxjs/operators');

const source$ = timer(0, 5).pipe(take(5));
const result$ = source$.pipe(single());

source$.subscribe(rxObserver('source'));
result$.subscribe(rxObserver('single()'));
0mssourcestartcomplete00 11 22 33 44 single()startSequence contains more than one element!

Predicate — predicate will narrow source values to just one:

1
2
3
4
5
6
7
8
9
10
const { rxObserver } = require('api/v0.3');
const { timer } = require('rxjs');
const { single, take } = require('rxjs/operators');

const source$ = timer(0, 5).pipe(take(5));
const result$ = source$.pipe(single(n => n === 3));

source$.subscribe(rxObserver('source'));
result$.subscribe(rxObserver('single(n => n === 3)'));
0mssourcestartcomplete00 11 22 33 44 single(n => n === 3)startcomplete33

An empty source — will result in an error, as no values are emitted at all:

1
2
3
4
5
6
7
8
9
10
const { rxObserver } = require('api/v0.3');
const { EMPTY } = require('rxjs');
const { single } = require('rxjs/operators');

const source$ = EMPTY;
const result$ = source$.pipe(single());

source$.subscribe(rxObserver('EMPTY'));
result$.subscribe(rxObserver('single()'));
0msEMPTYstartcompletesingle()startEmptyError: no elements in sequence!