Contents

combineLatest operator in RxJs

Updated on 2021-02-09

combineLatest Operator

Can be imported from here:

1
2
3
import { combineLatest } from 'rxjs';
// or
const { combineLatest } = require('rxjs');

For official documentation, visit https://rxjs.dev/api/operators/combineLatest

Combines multiple Observables to create an Observable whose values are calculated from the latest values of each of its input Observables.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import { take, map } from 'rxjs/operators';
import { interval, from, combineLatest } from 'rxjs';

const array1 = ['a', 'b', 'c', 'd', 'e'];
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];


const ob1$ = interval(2000).pipe(take(array1.length), map((i) => array1[i]));
const ob2$ = interval(1000).pipe(take(array2.length), map((i) => array2[i]));

const result$ = combineLatest(ob1$, ob2$);

result$.subscribe((val) => console.log(val));
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
["a", 1]
["a", 2]
["a", 3]
["b", 3]
["b", 4]
["b", 5]
["c", 5]
["c", 6]
["c", 7]
["d", 7]
["d", 8]
["d", 9]
["e", 9]
["e", 10]
/rxjs/combineAll.svg
combineLatest() Example

Комментарии