isEmpty
function
stable
Emits false
if the input Observable emits any values, or emits true
if the input Observable completes without emitting any values.
isEmpty<T>(): OperatorFunction<T, boolean>
Parameters
There are no parameters.
Returns
OperatorFunction<T, boolean>
: A function that returns an Observable that emits boolean value indicating whether the source Observable was empty or not.
Description
Tells whether any values are emitted by an Observable.
isEmpty
transforms an Observable that emits values into an Observable that emits a single boolean value representing whether or not any values were emitted by the source Observable. As soon as the source Observable emits a value, isEmpty
will emit a false
and complete. If the source Observable completes having not emitted anything, isEmpty
will emit a true
and complete.
A similar effect could be achieved with count
, but isEmpty
can emit a false
value sooner.
Examples
Emit false
for a non-empty Observable.
import { Subject } from 'rxjs'; import { isEmpty } from 'rxjs/operators'; const source = new Subject<string>(); const result = source.pipe(isEmpty()); source.subscribe(x => console.log(x)); result.subscribe(x => console.log(x)); source.next('a'); source.next('b'); source.next('c'); source.complete(); // Outputs // a // false // b // c
Emit true
for an empty Observable.
import { EMPTY } from 'rxjs'; import { isEmpty } from 'rxjs/operators'; const result = EMPTY.pipe(isEmpty()); result.subscribe(x => console.log(x)); // Outputs // true
See Also
© 2015–2021 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/isEmpty