signature: find(predicate: function)

Emit the first item that passes predicate then complete.

:bulb: If you always want the first item emitted, regardless of condition, try first()!


Example 1: Find click inside box, repeat when a click occurs outside of box

( StackBlitz)

// RxJS v6+
import { fromEvent } from 'rxjs';
import { find, repeatWhen, mapTo, startWith, filter } from 'rxjs/operators';

// elem ref
const status = document.getElementById('status');

// streams
const clicks$ = fromEvent(document, 'click');

    find((event: any) => === 'box'),
    startWith('Find me!'),
    // reset when click outside box
    repeatWhen(() =>
      clicks$.pipe(filter((event: any) => !== 'box'))
  .subscribe(message => (status.innerHTML = message));

Additional Resources

  • find :newspaper: - Official docs

:file_folder: Source Code:

results matching ""

    No results matching ""