Contents

concat operator in RxJs

Updated on 2021-02-09

concat Operator

Can be imported from here:

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

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

Concatenates output of observables.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import { of, concat } from 'rxjs';

concat(
  of(1, 2, 3),
  // subscribed after first completes
  of(4, 5, 6),
  // subscribed after second completes
  of(7, 8, 9)
)
  // log: 1, 2, 3, 4, 5, 6, 7, 8, 9
  .subscribe(console.log);
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import { take, map } from 'rxjs/operators';
import { interval, from, concat } 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$ = concat(ob1$, ob2$);

result$.subscribe((val) => console.log(val));
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
a
b
c
d
e
1
2
3
4
5
6
7
8
9
10

Комментарии