Learn RxJS
Search…
find

signature: find(predicate: function)

Emit the first item that passes predicate then complete.

πŸ’‘ If you always want the first item emitted, regardless of condition, try first()!
​​
​
​

Examples

Example 1: Find click inside box, repeat when a click occurs outside of box
1
// RxJS v6+
2
import { fromEvent } from 'rxjs';
3
import { find, repeatWhen, mapTo, startWith, filter } from 'rxjs/operators';
4
​
5
// elem ref
6
const status = document.getElementById('status');
7
​
8
// streams
9
const clicks$ = fromEvent(document, 'click');
10
​
11
clicks$
12
.pipe(
13
find((event: any) => event.target.id === 'box'),
14
mapTo('Found!'),
15
startWith('Find me!'),
16
// reset when click outside box
17
repeatWhen(() =>
18
clicks$.pipe(filter((event: any) => event.target.id !== 'box'))
19
)
20
)
21
.subscribe(message => (status.innerHTML = message));
Copied!

Additional Resources

  • ​find πŸ“° - Official docs
Last modified 1yr ago