-1

I create observables from HTTP requests in Angular 2. I want to run a piece of code if all observables complete without error.

Right now I'm using forkJoin to combine my array of observables

Observable.forkJoin(observables)
.subscribe(
  success => {
 //code
 },
 error => console.log('error'));
}

The issue I'm having is that forkJoin fires those HTTP-requests again. I already fired them when I created the observable, subscribed to it and put in an array. Is there an equivalent to forkJoin where I can run a piece of code after all observables complete, without firing the HTTP-requests again?

blid
  • 971
  • 13
  • 22
  • You could chain them using [flatMap](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/selectmany.md#rxobservableprototypeflatmapselector-resultselector) – alex kucksdorf Jun 13 '17 at 11:17
  • Or use [.do](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/do.md) instead of .forkJoin – hiper2d Jun 13 '17 at 11:18

1 Answers1

0

I think the problem is that you subscribe to those observables twice but, as they are cold observables they get fired twice.

In order to prevent this, I suggest you follow this thread that addresses this problem What is the correct way to share the result of an Angular 2 Http network call in RxJs 5?

atomrc
  • 2,543
  • 1
  • 16
  • 20