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

Is there another way of having an IF statement inside nested subscriptions using RxJS?

I need to assess the result of the first observable to determine if the second one should be subscribed to. From reading other threads and the RxJS documentation I can see that nested subscriptions should be avoided where possible. Below is an example:

    obs1().subscribe(result=> {
      if (result.status === 'success') {
        obs2().subscribe(() => {
          executeMethod();
        });
      }
    });

Any help or resources are greatly appreciated.

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 :

It could look like this:

obs1().pipe(
    filter(result => result.status === 'success'),
    switchMap(() =>  obs2()),
).subscribe( //...
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