Contents

forkJoin operator in RxJs

forkJoin Operator

Can be imported from here:

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

For official documentation, visit https://rxjs-dev.firebaseapp.com/api/operators/endWith

Returns an observable that takes in input array of observables or dictionary of observables and emits last values before completion of observables (in array or dictionary object).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import { forkJoin, of, timer } from 'rxjs';

const ob1$ = of(1, 2, 3, 4);
const ob2$ = Promise.resolve(8);
const ob3$ = timer(4000);
 
const result$ = forkJoin({
  foo: ob1$,
  bar: ob2$,
  baz: ob3$
});
result$.subscribe({
 next: value => console.log(value),
 complete: () => console.log('Completed!'),
});
1
2
{foo: 4, bar: 8, baz: 0}
Completed!

Note: result is emitted after 4 seconds as timer(4000) takes 4 seconds to complete.

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

const ob1$ = of(1, 2, 3, 4);
const ob2$ = Promise.resolve(8);
const ob3$ = timer(4000);
 
const result$ = forkJoin([ob1$, ob2$, ob3$]);
result$.subscribe({
 next: value => console.log(value),
 complete: () => console.log('Completed!'),
});
1
2
[4, 8, 0]
Completed!

Note: result is emitted after 4 seconds as timer(4000) takes 4 seconds to complete.

Комментарии