Contents

merge operator in RxJs

merge Operator

Can be imported from here:

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

For official documentation, visit https://rxjs-dev.firebaseapp.com/api/index/function/merge

Returns observable that emits values from all input observables.

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

const array1 = ['a', 'b', 'c', 'd', 'e'];
const array2 = [1, 2, 3, 4, 5];

const ob1$ = interval(1000).pipe(take(array1.length), map((i) => array1[i]));
const ob2$ = interval(1001).pipe(take(array2.length), map((i) => array2[i]));
 
const result$ = merge(ob1$, ob2$);
result$.subscribe({
 next: value => console.log(value),
 complete: () => console.log('Completed!'),
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
a
1
b
2
c
3
d
4
e
5
Completed!

Комментарии