RxJs: ConcatMap VS ConcatMapTo, MergeMap VS MergeMapTo









up vote
1
down vote

favorite












The documentation isn't helpful enough for me to understand the difference between these.




It's like concatMap, but maps each value always to the same inner
Observable.
http://reactivex.io/rxjs/file/es6/operators/concatMapTo.js.html




I tried checking out learnrxjs.io's examples on stackblitz, but even with those, I wasn't able to immediately identify what the distinguishing feature was separating these.



FYI i saw this other similar question
What is the difference between mergeMap and mergeMapTo?
but the answer in there wasn't satisfactory, because in the learnrxjs.io examples, they clearly map to observables, not hard-coded values.
https://www.learnrxjs.io/operators/transformation/concatmapto.html



If someone could provide some examples (and maybe a brief explanation) to help distinguish the *** vs the ***To higher-order observable operators, I'd appreciate that, thanks.










share|improve this question

























    up vote
    1
    down vote

    favorite












    The documentation isn't helpful enough for me to understand the difference between these.




    It's like concatMap, but maps each value always to the same inner
    Observable.
    http://reactivex.io/rxjs/file/es6/operators/concatMapTo.js.html




    I tried checking out learnrxjs.io's examples on stackblitz, but even with those, I wasn't able to immediately identify what the distinguishing feature was separating these.



    FYI i saw this other similar question
    What is the difference between mergeMap and mergeMapTo?
    but the answer in there wasn't satisfactory, because in the learnrxjs.io examples, they clearly map to observables, not hard-coded values.
    https://www.learnrxjs.io/operators/transformation/concatmapto.html



    If someone could provide some examples (and maybe a brief explanation) to help distinguish the *** vs the ***To higher-order observable operators, I'd appreciate that, thanks.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      The documentation isn't helpful enough for me to understand the difference between these.




      It's like concatMap, but maps each value always to the same inner
      Observable.
      http://reactivex.io/rxjs/file/es6/operators/concatMapTo.js.html




      I tried checking out learnrxjs.io's examples on stackblitz, but even with those, I wasn't able to immediately identify what the distinguishing feature was separating these.



      FYI i saw this other similar question
      What is the difference between mergeMap and mergeMapTo?
      but the answer in there wasn't satisfactory, because in the learnrxjs.io examples, they clearly map to observables, not hard-coded values.
      https://www.learnrxjs.io/operators/transformation/concatmapto.html



      If someone could provide some examples (and maybe a brief explanation) to help distinguish the *** vs the ***To higher-order observable operators, I'd appreciate that, thanks.










      share|improve this question













      The documentation isn't helpful enough for me to understand the difference between these.




      It's like concatMap, but maps each value always to the same inner
      Observable.
      http://reactivex.io/rxjs/file/es6/operators/concatMapTo.js.html




      I tried checking out learnrxjs.io's examples on stackblitz, but even with those, I wasn't able to immediately identify what the distinguishing feature was separating these.



      FYI i saw this other similar question
      What is the difference between mergeMap and mergeMapTo?
      but the answer in there wasn't satisfactory, because in the learnrxjs.io examples, they clearly map to observables, not hard-coded values.
      https://www.learnrxjs.io/operators/transformation/concatmapto.html



      If someone could provide some examples (and maybe a brief explanation) to help distinguish the *** vs the ***To higher-order observable operators, I'd appreciate that, thanks.







      rxjs rxjs6






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 14:48









      squirrelsareduck

      3811312




      3811312






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Simply said, variants with *To will always use the same Observable that needs to be created when the whole chain is created regardless of the values emitted by the chain. They take an Observable as a parameter.



          Variants without *To can create and return any Observable only when their source Observable emits. They take a callback as a parameter.



          For example when I use mergeMapTo I'm always subscribing to the same Observable:



          source.pipe(
          mergeMapTo(of(1)),
          )


          Every emission from source will always be mapped to of(1) and there's no way I can change that.



          One the other hand with just mergeMap I can return any Observable I want depending on the value received:



          source.pipe(
          mergeMap(v => of(v * 2)),
          )


          Maybe an easier way to think about this is to remember that *To variants map a value to a constant (even when it's not a "real JavaScript constant").






          share|improve this answer


















          • 1




            thanks! took me a while to understand your answer, but now i get it. the *To ones take an observable as the direct argument, whereas the ones without the *To suffix take a function as the direct argument (and that function could conditionally return various, different observables depending on given conditional logic).
            – squirrelsareduck
            Nov 10 at 15:42










          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',
          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%2f53240090%2frxjs-concatmap-vs-concatmapto-mergemap-vs-mergemapto%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          Simply said, variants with *To will always use the same Observable that needs to be created when the whole chain is created regardless of the values emitted by the chain. They take an Observable as a parameter.



          Variants without *To can create and return any Observable only when their source Observable emits. They take a callback as a parameter.



          For example when I use mergeMapTo I'm always subscribing to the same Observable:



          source.pipe(
          mergeMapTo(of(1)),
          )


          Every emission from source will always be mapped to of(1) and there's no way I can change that.



          One the other hand with just mergeMap I can return any Observable I want depending on the value received:



          source.pipe(
          mergeMap(v => of(v * 2)),
          )


          Maybe an easier way to think about this is to remember that *To variants map a value to a constant (even when it's not a "real JavaScript constant").






          share|improve this answer


















          • 1




            thanks! took me a while to understand your answer, but now i get it. the *To ones take an observable as the direct argument, whereas the ones without the *To suffix take a function as the direct argument (and that function could conditionally return various, different observables depending on given conditional logic).
            – squirrelsareduck
            Nov 10 at 15:42














          up vote
          3
          down vote



          accepted










          Simply said, variants with *To will always use the same Observable that needs to be created when the whole chain is created regardless of the values emitted by the chain. They take an Observable as a parameter.



          Variants without *To can create and return any Observable only when their source Observable emits. They take a callback as a parameter.



          For example when I use mergeMapTo I'm always subscribing to the same Observable:



          source.pipe(
          mergeMapTo(of(1)),
          )


          Every emission from source will always be mapped to of(1) and there's no way I can change that.



          One the other hand with just mergeMap I can return any Observable I want depending on the value received:



          source.pipe(
          mergeMap(v => of(v * 2)),
          )


          Maybe an easier way to think about this is to remember that *To variants map a value to a constant (even when it's not a "real JavaScript constant").






          share|improve this answer


















          • 1




            thanks! took me a while to understand your answer, but now i get it. the *To ones take an observable as the direct argument, whereas the ones without the *To suffix take a function as the direct argument (and that function could conditionally return various, different observables depending on given conditional logic).
            – squirrelsareduck
            Nov 10 at 15:42












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Simply said, variants with *To will always use the same Observable that needs to be created when the whole chain is created regardless of the values emitted by the chain. They take an Observable as a parameter.



          Variants without *To can create and return any Observable only when their source Observable emits. They take a callback as a parameter.



          For example when I use mergeMapTo I'm always subscribing to the same Observable:



          source.pipe(
          mergeMapTo(of(1)),
          )


          Every emission from source will always be mapped to of(1) and there's no way I can change that.



          One the other hand with just mergeMap I can return any Observable I want depending on the value received:



          source.pipe(
          mergeMap(v => of(v * 2)),
          )


          Maybe an easier way to think about this is to remember that *To variants map a value to a constant (even when it's not a "real JavaScript constant").






          share|improve this answer














          Simply said, variants with *To will always use the same Observable that needs to be created when the whole chain is created regardless of the values emitted by the chain. They take an Observable as a parameter.



          Variants without *To can create and return any Observable only when their source Observable emits. They take a callback as a parameter.



          For example when I use mergeMapTo I'm always subscribing to the same Observable:



          source.pipe(
          mergeMapTo(of(1)),
          )


          Every emission from source will always be mapped to of(1) and there's no way I can change that.



          One the other hand with just mergeMap I can return any Observable I want depending on the value received:



          source.pipe(
          mergeMap(v => of(v * 2)),
          )


          Maybe an easier way to think about this is to remember that *To variants map a value to a constant (even when it's not a "real JavaScript constant").







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 16:18

























          answered Nov 10 at 15:02









          martin

          39.8k1180124




          39.8k1180124







          • 1




            thanks! took me a while to understand your answer, but now i get it. the *To ones take an observable as the direct argument, whereas the ones without the *To suffix take a function as the direct argument (and that function could conditionally return various, different observables depending on given conditional logic).
            – squirrelsareduck
            Nov 10 at 15:42












          • 1




            thanks! took me a while to understand your answer, but now i get it. the *To ones take an observable as the direct argument, whereas the ones without the *To suffix take a function as the direct argument (and that function could conditionally return various, different observables depending on given conditional logic).
            – squirrelsareduck
            Nov 10 at 15:42







          1




          1




          thanks! took me a while to understand your answer, but now i get it. the *To ones take an observable as the direct argument, whereas the ones without the *To suffix take a function as the direct argument (and that function could conditionally return various, different observables depending on given conditional logic).
          – squirrelsareduck
          Nov 10 at 15:42




          thanks! took me a while to understand your answer, but now i get it. the *To ones take an observable as the direct argument, whereas the ones without the *To suffix take a function as the direct argument (and that function could conditionally return various, different observables depending on given conditional logic).
          – squirrelsareduck
          Nov 10 at 15:42

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240090%2frxjs-concatmap-vs-concatmapto-mergemap-vs-mergemapto%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







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線