Learn RxJS
Search…
ignoreElements

signature: ignoreElements(): Observable

Ignore everything but complete and error.

Examples

Example 1: Ignore all elements from source
1
// RxJS v6+
2
import { interval } from 'rxjs';
3
import { take, ignoreElements } from 'rxjs/operators';
4
5
//emit value every 100ms
6
const source = interval(100);
7
//ignore everything but complete
8
const example = source.pipe(take(5), ignoreElements());
9
//output: "COMPLETE!"
10
const subscribe = example.subscribe(
11
val => console.log(`NEXT: ${val}`),
12
val => console.log(`ERROR: ${val}`),
13
() => console.log('COMPLETE!')
14
);
Copied!
Example 2: Only displaying error
1
// RxJS v6+
2
import { interval, throwError, of } from 'rxjs';
3
import { mergeMap, ignoreElements } from 'rxjs/operators';
4
5
//emit value every 100ms
6
const source = interval(100);
7
//ignore everything but error
8
const error = source.pipe(
9
mergeMap(val => {
10
if (val === 4) {
11
return throwError(`ERROR AT ${val}`);
12
}
13
return of(val);
14
}),
15
ignoreElements()
16
);
17
//output: "ERROR: ERROR AT 4"
18
const subscribe = error.subscribe(
19
val => console.log(`NEXT: ${val}`),
20
val => console.log(`ERROR: ${val}`),
21
() => console.log('SECOND COMPLETE!')
22
);
Copied!

Additional Resources

Last modified 2yr ago