Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can I subscribe inside the combineLatest by the return result?

I want to use combineLatest to get a bunch of result. Then one of result is passed as input parameter to do another subscribe call.

combineLatest([first, second, third, four, five]).pipe(take(1)).
   subscribe(([a, b, c, d, e]) => {
        this.showData1(a);
        this.showData2(b);
        this.showData3(c);
        this.showData4(d);
        if(e > 8) {
           this.myService.updateStatus(e).subscribe(
              result => this.setupStatus(result)
           );
       }
   });

The logic is from the 5 results, 4 of them are used for rendering screen. The last one I use it to do a subscribe, its returning result will used some where.

But I feel that it is not right because sometimes it seems this.setupStatus(result) is not running inside the combineLatest. Memory leak?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

hard to diagnose why it wouldn’t be running without knowing more about the observables. but I’d do it something like this to avoid nested subscribes and make this a little easier to diagnose / manage:

combineLatest([first, second, third, four, five]).pipe(
   take(1),
   switchMap(([a, b, c, d, e]) => {
        this.showData1(a);
        this.showData2(b);
        this.showData3(c);
        this.showData4(d);
        return this.myService.updateStatus(e);
   })
).subscribe(result => this.setupStatus(result));

possibly structuring a little differently depending on when exactly I want things to happen.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading