ignoreElements

signature: ignoreElements(): Observable

Ignore everything but complete and error.

ignoreElements - 图1

Examples

Example 1: Ignore all elements from source

( jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { take, ignoreElements } from 'rxjs/operators';
  3. //emit value every 100ms
  4. const source = interval(100);
  5. //ignore everything but complete
  6. const example = source.pipe(take(5), ignoreElements());
  7. //output: "COMPLETE!"
  8. const subscribe = example.subscribe(
  9. val => console.log(`NEXT: ${val}`),
  10. val => console.log(`ERROR: ${val}`),
  11. () => console.log('COMPLETE!')
  12. );
Example 2: Only displaying error

( jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { _throw } from 'rxjs/observable/throw';
  3. import { of } from 'rxjs/observable/of';
  4. import { mergeMap, ignoreElements } from 'rxjs/operators';
  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 _throw(`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. );

Additional Resources


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