Learn RxJS
Search…
every

signature: every(predicate: function, thisArg: any): Observable

If all values pass predicate before completion emit true, else false.

Examples

Example 1: Some values false
1
// RxJS v6+
2
import { every } from 'rxjs/operators';
3
import { of } from 'rxjs';
4
5
//emit 5 values
6
const source = of(1, 2, 3, 4, 5);
7
const example = source.pipe(
8
//is every value even?
9
every(val => val % 2 === 0)
10
);
11
//output: false
12
const subscribe = example.subscribe(val => console.log(val));
Copied!
Example 2: All values true
1
// RxJS v6+
2
import { every } from 'rxjs/operators';
3
import { of } from 'rxjs';
4
5
//emit 5 values
6
const allEvens = of(2, 4, 6, 8, 10);
7
const example = allEvens.pipe(
8
//is every value even?
9
every(val => val % 2 === 0)
10
);
11
//output: true
12
const subscribe = example.subscribe(val => console.log(val));
Copied!
Example 3: Values arriving over time and completing stream prematurely due to every returning false
1
// RxJS v6+
2
console.clear();
3
import { concat, of } from 'rxjs';
4
import { every, delay, tap } from 'rxjs/operators';
5
6
const log = console.log;
7
const returnCode = request => (Number.isInteger(request) ? 200 : 400);
8
const fakeRequest = request =>
9
of({ code: returnCode(request) }).pipe(
10
tap(_ => log(request)),
11
delay(1000)
12
);
13
14
const apiCalls$ = concat(
15
fakeRequest(1),
16
fakeRequest('invalid payload'),
17
fakeRequest(2) //this won't execute as every will return false for previous line
18
).pipe(
19
every(e => e.code === 200),
20
tap(e => log(`all request successful: ${e}`))
21
);
22
23
apiCalls$.subscribe();
Copied!

Additional Resources

  • every 📰 - Official docs
Last modified 2yr ago