How to get result of Observer in subscribe?











up vote
0
down vote

favorite
1












I have two Observable requests to server:



this.initService.__init__(res).subscribe((profile) => {
console.log(profile); // It works
this.initService.initializePeriod(profile).subscribe((period) => {
console.log(period); // It does not work
});

});


When first gets response it calls second, but I can not get result for second subscribe. I know, better to use Promises in this case, but I need pass result from first request to second, promises dont allow to do this.



I tried to use this, but Alert message does not work:



 this.initService.__init__(res)
.pipe(
mergeMap(profile => this.initService.initializePeriod(profile)))
.subscribe((response) => {
alert('aa');

});


Also tried this:



this.initService.__init__(res)
.mergeMap(profile => this.initService.initializePeriod(profile))
.subscribe((response) => {
alert('aa');
});









share|improve this question




























    up vote
    0
    down vote

    favorite
    1












    I have two Observable requests to server:



    this.initService.__init__(res).subscribe((profile) => {
    console.log(profile); // It works
    this.initService.initializePeriod(profile).subscribe((period) => {
    console.log(period); // It does not work
    });

    });


    When first gets response it calls second, but I can not get result for second subscribe. I know, better to use Promises in this case, but I need pass result from first request to second, promises dont allow to do this.



    I tried to use this, but Alert message does not work:



     this.initService.__init__(res)
    .pipe(
    mergeMap(profile => this.initService.initializePeriod(profile)))
    .subscribe((response) => {
    alert('aa');

    });


    Also tried this:



    this.initService.__init__(res)
    .mergeMap(profile => this.initService.initializePeriod(profile))
    .subscribe((response) => {
    alert('aa');
    });









    share|improve this question


























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have two Observable requests to server:



      this.initService.__init__(res).subscribe((profile) => {
      console.log(profile); // It works
      this.initService.initializePeriod(profile).subscribe((period) => {
      console.log(period); // It does not work
      });

      });


      When first gets response it calls second, but I can not get result for second subscribe. I know, better to use Promises in this case, but I need pass result from first request to second, promises dont allow to do this.



      I tried to use this, but Alert message does not work:



       this.initService.__init__(res)
      .pipe(
      mergeMap(profile => this.initService.initializePeriod(profile)))
      .subscribe((response) => {
      alert('aa');

      });


      Also tried this:



      this.initService.__init__(res)
      .mergeMap(profile => this.initService.initializePeriod(profile))
      .subscribe((response) => {
      alert('aa');
      });









      share|improve this question















      I have two Observable requests to server:



      this.initService.__init__(res).subscribe((profile) => {
      console.log(profile); // It works
      this.initService.initializePeriod(profile).subscribe((period) => {
      console.log(period); // It does not work
      });

      });


      When first gets response it calls second, but I can not get result for second subscribe. I know, better to use Promises in this case, but I need pass result from first request to second, promises dont allow to do this.



      I tried to use this, but Alert message does not work:



       this.initService.__init__(res)
      .pipe(
      mergeMap(profile => this.initService.initializePeriod(profile)))
      .subscribe((response) => {
      alert('aa');

      });


      Also tried this:



      this.initService.__init__(res)
      .mergeMap(profile => this.initService.initializePeriod(profile))
      .subscribe((response) => {
      alert('aa');
      });






      angular rxjs rxjs5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 14:45

























      asked Nov 10 at 13:30









      OPV

      1,46821329




      1,46821329
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You can use switchMap rxjs operators to chain the observables.



          import { switchMap } from 'rxjs/operators';

          this.initService.__init__(res)
          .pipe(
          switchMap(profile=> this.initService.initializePeriod(profile))
          ).subscribe((period) => {
          console.log(period);
          });


          If you want to have response from both then use mergeMap



          import { mergeMap } from 'rxjs/operators';

          this.initService.__init__(res)
          .pipe(
          mergeMap(
          profile=> this.initService.initializePeriod(profile),
          (profile, period) => {
          return [profile,period];
          }
          )
          ).subscribe((response) => {
          console.log(response[0]); //profile
          console.log(response[1]); //period
          });


          Working copy is here - https://stackblitz.com/edit/typescript-wy26a7






          share|improve this answer























          • Okay, how to transmit profile variable inside last subscribe?
            – OPV
            Nov 10 at 13:49










          • You can use mergeMap, updated the answer.
            – Sunil Singh
            Nov 10 at 13:58










          • I see two requests are working, but I still can not get common result, I do alert() in response, but dont get it
            – OPV
            Nov 10 at 14:26










          • It is very weird line: ``` profile => this.initService.initializePeriod(profile), (profile, period) => { return [profile, period]; } ```
            – OPV
            Nov 10 at 14:38










          • I tried this. but alert does not work: ` this.initService.__init__(res) .pipe( mergeMap(profile => this.initService.initializePeriod(profile))) .subscribe((response) => { alert('aa'); `
            – OPV
            Nov 10 at 14:45


















          up vote
          0
          down vote













          You need to check the result that you are getting from your first API call (profile). Check your browser console. Chances are that the profile is probably a JSON, not a valid URL. The request will not go through if the URL is not valid.



          If you provide a valid URL, API call inside another API call should work.






          share|improve this answer





















          • I checked this, both return response from server
            – OPV
            Nov 10 at 16:17








          • 1




            I am assuming that the function calls : initService.__init__ () and initService.initializePeriod() is only for minimal reproduction and you are actually making the http calls at that place. Is this correct ?
            – Sachin Gupta
            Nov 10 at 16:23












          • I guess problem is in HTTP interceptor, I will check
            – OPV
            Nov 10 at 17:00











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239457%2fhow-to-get-result-of-observer-in-subscribe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          You can use switchMap rxjs operators to chain the observables.



          import { switchMap } from 'rxjs/operators';

          this.initService.__init__(res)
          .pipe(
          switchMap(profile=> this.initService.initializePeriod(profile))
          ).subscribe((period) => {
          console.log(period);
          });


          If you want to have response from both then use mergeMap



          import { mergeMap } from 'rxjs/operators';

          this.initService.__init__(res)
          .pipe(
          mergeMap(
          profile=> this.initService.initializePeriod(profile),
          (profile, period) => {
          return [profile,period];
          }
          )
          ).subscribe((response) => {
          console.log(response[0]); //profile
          console.log(response[1]); //period
          });


          Working copy is here - https://stackblitz.com/edit/typescript-wy26a7






          share|improve this answer























          • Okay, how to transmit profile variable inside last subscribe?
            – OPV
            Nov 10 at 13:49










          • You can use mergeMap, updated the answer.
            – Sunil Singh
            Nov 10 at 13:58










          • I see two requests are working, but I still can not get common result, I do alert() in response, but dont get it
            – OPV
            Nov 10 at 14:26










          • It is very weird line: ``` profile => this.initService.initializePeriod(profile), (profile, period) => { return [profile, period]; } ```
            – OPV
            Nov 10 at 14:38










          • I tried this. but alert does not work: ` this.initService.__init__(res) .pipe( mergeMap(profile => this.initService.initializePeriod(profile))) .subscribe((response) => { alert('aa'); `
            – OPV
            Nov 10 at 14:45















          up vote
          2
          down vote



          accepted










          You can use switchMap rxjs operators to chain the observables.



          import { switchMap } from 'rxjs/operators';

          this.initService.__init__(res)
          .pipe(
          switchMap(profile=> this.initService.initializePeriod(profile))
          ).subscribe((period) => {
          console.log(period);
          });


          If you want to have response from both then use mergeMap



          import { mergeMap } from 'rxjs/operators';

          this.initService.__init__(res)
          .pipe(
          mergeMap(
          profile=> this.initService.initializePeriod(profile),
          (profile, period) => {
          return [profile,period];
          }
          )
          ).subscribe((response) => {
          console.log(response[0]); //profile
          console.log(response[1]); //period
          });


          Working copy is here - https://stackblitz.com/edit/typescript-wy26a7






          share|improve this answer























          • Okay, how to transmit profile variable inside last subscribe?
            – OPV
            Nov 10 at 13:49










          • You can use mergeMap, updated the answer.
            – Sunil Singh
            Nov 10 at 13:58










          • I see two requests are working, but I still can not get common result, I do alert() in response, but dont get it
            – OPV
            Nov 10 at 14:26










          • It is very weird line: ``` profile => this.initService.initializePeriod(profile), (profile, period) => { return [profile, period]; } ```
            – OPV
            Nov 10 at 14:38










          • I tried this. but alert does not work: ` this.initService.__init__(res) .pipe( mergeMap(profile => this.initService.initializePeriod(profile))) .subscribe((response) => { alert('aa'); `
            – OPV
            Nov 10 at 14:45













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You can use switchMap rxjs operators to chain the observables.



          import { switchMap } from 'rxjs/operators';

          this.initService.__init__(res)
          .pipe(
          switchMap(profile=> this.initService.initializePeriod(profile))
          ).subscribe((period) => {
          console.log(period);
          });


          If you want to have response from both then use mergeMap



          import { mergeMap } from 'rxjs/operators';

          this.initService.__init__(res)
          .pipe(
          mergeMap(
          profile=> this.initService.initializePeriod(profile),
          (profile, period) => {
          return [profile,period];
          }
          )
          ).subscribe((response) => {
          console.log(response[0]); //profile
          console.log(response[1]); //period
          });


          Working copy is here - https://stackblitz.com/edit/typescript-wy26a7






          share|improve this answer














          You can use switchMap rxjs operators to chain the observables.



          import { switchMap } from 'rxjs/operators';

          this.initService.__init__(res)
          .pipe(
          switchMap(profile=> this.initService.initializePeriod(profile))
          ).subscribe((period) => {
          console.log(period);
          });


          If you want to have response from both then use mergeMap



          import { mergeMap } from 'rxjs/operators';

          this.initService.__init__(res)
          .pipe(
          mergeMap(
          profile=> this.initService.initializePeriod(profile),
          (profile, period) => {
          return [profile,period];
          }
          )
          ).subscribe((response) => {
          console.log(response[0]); //profile
          console.log(response[1]); //period
          });


          Working copy is here - https://stackblitz.com/edit/typescript-wy26a7







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 15:50

























          answered Nov 10 at 13:34









          Sunil Singh

          6,1372626




          6,1372626












          • Okay, how to transmit profile variable inside last subscribe?
            – OPV
            Nov 10 at 13:49










          • You can use mergeMap, updated the answer.
            – Sunil Singh
            Nov 10 at 13:58










          • I see two requests are working, but I still can not get common result, I do alert() in response, but dont get it
            – OPV
            Nov 10 at 14:26










          • It is very weird line: ``` profile => this.initService.initializePeriod(profile), (profile, period) => { return [profile, period]; } ```
            – OPV
            Nov 10 at 14:38










          • I tried this. but alert does not work: ` this.initService.__init__(res) .pipe( mergeMap(profile => this.initService.initializePeriod(profile))) .subscribe((response) => { alert('aa'); `
            – OPV
            Nov 10 at 14:45


















          • Okay, how to transmit profile variable inside last subscribe?
            – OPV
            Nov 10 at 13:49










          • You can use mergeMap, updated the answer.
            – Sunil Singh
            Nov 10 at 13:58










          • I see two requests are working, but I still can not get common result, I do alert() in response, but dont get it
            – OPV
            Nov 10 at 14:26










          • It is very weird line: ``` profile => this.initService.initializePeriod(profile), (profile, period) => { return [profile, period]; } ```
            – OPV
            Nov 10 at 14:38










          • I tried this. but alert does not work: ` this.initService.__init__(res) .pipe( mergeMap(profile => this.initService.initializePeriod(profile))) .subscribe((response) => { alert('aa'); `
            – OPV
            Nov 10 at 14:45
















          Okay, how to transmit profile variable inside last subscribe?
          – OPV
          Nov 10 at 13:49




          Okay, how to transmit profile variable inside last subscribe?
          – OPV
          Nov 10 at 13:49












          You can use mergeMap, updated the answer.
          – Sunil Singh
          Nov 10 at 13:58




          You can use mergeMap, updated the answer.
          – Sunil Singh
          Nov 10 at 13:58












          I see two requests are working, but I still can not get common result, I do alert() in response, but dont get it
          – OPV
          Nov 10 at 14:26




          I see two requests are working, but I still can not get common result, I do alert() in response, but dont get it
          – OPV
          Nov 10 at 14:26












          It is very weird line: ``` profile => this.initService.initializePeriod(profile), (profile, period) => { return [profile, period]; } ```
          – OPV
          Nov 10 at 14:38




          It is very weird line: ``` profile => this.initService.initializePeriod(profile), (profile, period) => { return [profile, period]; } ```
          – OPV
          Nov 10 at 14:38












          I tried this. but alert does not work: ` this.initService.__init__(res) .pipe( mergeMap(profile => this.initService.initializePeriod(profile))) .subscribe((response) => { alert('aa'); `
          – OPV
          Nov 10 at 14:45




          I tried this. but alert does not work: ` this.initService.__init__(res) .pipe( mergeMap(profile => this.initService.initializePeriod(profile))) .subscribe((response) => { alert('aa'); `
          – OPV
          Nov 10 at 14:45












          up vote
          0
          down vote













          You need to check the result that you are getting from your first API call (profile). Check your browser console. Chances are that the profile is probably a JSON, not a valid URL. The request will not go through if the URL is not valid.



          If you provide a valid URL, API call inside another API call should work.






          share|improve this answer





















          • I checked this, both return response from server
            – OPV
            Nov 10 at 16:17








          • 1




            I am assuming that the function calls : initService.__init__ () and initService.initializePeriod() is only for minimal reproduction and you are actually making the http calls at that place. Is this correct ?
            – Sachin Gupta
            Nov 10 at 16:23












          • I guess problem is in HTTP interceptor, I will check
            – OPV
            Nov 10 at 17:00















          up vote
          0
          down vote













          You need to check the result that you are getting from your first API call (profile). Check your browser console. Chances are that the profile is probably a JSON, not a valid URL. The request will not go through if the URL is not valid.



          If you provide a valid URL, API call inside another API call should work.






          share|improve this answer





















          • I checked this, both return response from server
            – OPV
            Nov 10 at 16:17








          • 1




            I am assuming that the function calls : initService.__init__ () and initService.initializePeriod() is only for minimal reproduction and you are actually making the http calls at that place. Is this correct ?
            – Sachin Gupta
            Nov 10 at 16:23












          • I guess problem is in HTTP interceptor, I will check
            – OPV
            Nov 10 at 17:00













          up vote
          0
          down vote










          up vote
          0
          down vote









          You need to check the result that you are getting from your first API call (profile). Check your browser console. Chances are that the profile is probably a JSON, not a valid URL. The request will not go through if the URL is not valid.



          If you provide a valid URL, API call inside another API call should work.






          share|improve this answer












          You need to check the result that you are getting from your first API call (profile). Check your browser console. Chances are that the profile is probably a JSON, not a valid URL. The request will not go through if the URL is not valid.



          If you provide a valid URL, API call inside another API call should work.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 16:16









          Sachin Gupta

          42428




          42428












          • I checked this, both return response from server
            – OPV
            Nov 10 at 16:17








          • 1




            I am assuming that the function calls : initService.__init__ () and initService.initializePeriod() is only for minimal reproduction and you are actually making the http calls at that place. Is this correct ?
            – Sachin Gupta
            Nov 10 at 16:23












          • I guess problem is in HTTP interceptor, I will check
            – OPV
            Nov 10 at 17:00


















          • I checked this, both return response from server
            – OPV
            Nov 10 at 16:17








          • 1




            I am assuming that the function calls : initService.__init__ () and initService.initializePeriod() is only for minimal reproduction and you are actually making the http calls at that place. Is this correct ?
            – Sachin Gupta
            Nov 10 at 16:23












          • I guess problem is in HTTP interceptor, I will check
            – OPV
            Nov 10 at 17:00
















          I checked this, both return response from server
          – OPV
          Nov 10 at 16:17






          I checked this, both return response from server
          – OPV
          Nov 10 at 16:17






          1




          1




          I am assuming that the function calls : initService.__init__ () and initService.initializePeriod() is only for minimal reproduction and you are actually making the http calls at that place. Is this correct ?
          – Sachin Gupta
          Nov 10 at 16:23






          I am assuming that the function calls : initService.__init__ () and initService.initializePeriod() is only for minimal reproduction and you are actually making the http calls at that place. Is this correct ?
          – Sachin Gupta
          Nov 10 at 16:23














          I guess problem is in HTTP interceptor, I will check
          – OPV
          Nov 10 at 17:00




          I guess problem is in HTTP interceptor, I will check
          – OPV
          Nov 10 at 17:00


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239457%2fhow-to-get-result-of-observer-in-subscribe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini