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

how to sequence run multiple observables with different data types and logics

hello guys in my case i have a form that includes multiple select types,options of these inputs come form API and then feed the emitted data from HTTP request observable to my FormGroup every thing works fine by now.

then decided to add edit feature to my app, so user can edit related entity with the same component. so i need to run two observable one after each and i need to first one to be completed to run another one.

it means i need to call getFormFieldData form my service feed its emitted value to my formGroup then after this i need to call getUserEntityData() and feed its emitted data to my formGroup initial value, after this user can start editing.

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

there must be a rxjs operator to do this i read some articles about concat but types of my emitted data are different and also the logic.

tnx for helping me.

>Solution :

Your looking for switchMap on the high level it will look like

doAction() {
    // disable form
    this.service.getFormFieldData().pipe(
       switchMap((response) => {
           this.formGroup.patchValue(response);
           return this.service.getUserEntityData().pipe(
               map((response2) => {
                    this.formGroup.patchValue(response2);
               })
           );
       })
    ).subscribe(() => {
        // enable form
    });
}
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