How can I know if Observable has finalized with error or without error?









up vote
2
down vote

favorite
1












I need to execute some code when Observable is completed depending on whether has finalized with error or without. I have this code:



const obs = getMyObservable().pipe(finalize(() => 
//here
));


As you see, I'm using finalize operator, but I can't know if has finalized with or without error. Is there some kind of doOnComplete or doOnError operators in rxjs?










share|improve this question

























    up vote
    2
    down vote

    favorite
    1












    I need to execute some code when Observable is completed depending on whether has finalized with error or without. I have this code:



    const obs = getMyObservable().pipe(finalize(() => 
    //here
    ));


    As you see, I'm using finalize operator, but I can't know if has finalized with or without error. Is there some kind of doOnComplete or doOnError operators in rxjs?










    share|improve this question























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I need to execute some code when Observable is completed depending on whether has finalized with error or without. I have this code:



      const obs = getMyObservable().pipe(finalize(() => 
      //here
      ));


      As you see, I'm using finalize operator, but I can't know if has finalized with or without error. Is there some kind of doOnComplete or doOnError operators in rxjs?










      share|improve this question













      I need to execute some code when Observable is completed depending on whether has finalized with error or without. I have this code:



      const obs = getMyObservable().pipe(finalize(() => 
      //here
      ));


      As you see, I'm using finalize operator, but I can't know if has finalized with or without error. Is there some kind of doOnComplete or doOnError operators in rxjs?







      javascript rxjs reactive-streams






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 18 at 10:48









      Héctor

      9,0891358124




      9,0891358124






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          According to https://www.learnrxjs.io/



          With the latest RXJS you can use this 3 operators



          const obs = getMyObservable().pipe( // Let's assume 'obs' returns an array
          tap(() => console.log('Action performed before any other')),
          catchError(() => console.error('Error emitted'); return of(); ), // We return instead
          finalize(() => console.log('Action to be executed always')) // Either Error or Success
          );
          obs.subscribe(data => console.log(data)); // After everything, we log the output.


          Hope it helps



          EDIT based on JoniJnm comment



          To be More specific, there are, 3 main Pipes:



          1. Pipes that does alter the result before subscription.

          2. Pipes that does not alter the result before subscription.

          3. Special Pipes.

          Tap for example is from the second type, it can takes the input from the observable or previous pipes and do anything with it but cannot change the result of the pipe for the next step.



          Map is similar but it belongs to the first type of Pipe, it takes an input and has to return an output that can be used in the next pipe or final subscription.



          Finalise is a special pipe, it does the same than Tap but after subscription, it is good for example to log final results or to cancel subscription after it completes.



          CatchError is a pipe which alters the result but it is only called if the previous step has thrown an error. This is used to avoid unhandled error and you should return an observable "default" instead of the failed observable (so we handle the error and the app does not break).



          You can guess when your observable had an Error if catchError has been launched and handle it straight away before it reach the subscription.



          If this pipe is not launched, the result is considered without error.






          share|improve this answer






















          • This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
            – JoniJnm
            Oct 29 at 17:28






          • 1




            The order of execution is Observable -> Pipes in order (except finalize) -> subscribe -> finalize Pipe with this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
            – Ignacio Bustos
            Nov 11 at 16:02










          • So something like this: stackblitz.com/edit/typescript-kpusau
            – JoniJnm
            Nov 22 at 8:54










          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%2f50409658%2fhow-can-i-know-if-observable-has-finalized-with-error-or-without-error%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
          1
          down vote



          accepted










          According to https://www.learnrxjs.io/



          With the latest RXJS you can use this 3 operators



          const obs = getMyObservable().pipe( // Let's assume 'obs' returns an array
          tap(() => console.log('Action performed before any other')),
          catchError(() => console.error('Error emitted'); return of(); ), // We return instead
          finalize(() => console.log('Action to be executed always')) // Either Error or Success
          );
          obs.subscribe(data => console.log(data)); // After everything, we log the output.


          Hope it helps



          EDIT based on JoniJnm comment



          To be More specific, there are, 3 main Pipes:



          1. Pipes that does alter the result before subscription.

          2. Pipes that does not alter the result before subscription.

          3. Special Pipes.

          Tap for example is from the second type, it can takes the input from the observable or previous pipes and do anything with it but cannot change the result of the pipe for the next step.



          Map is similar but it belongs to the first type of Pipe, it takes an input and has to return an output that can be used in the next pipe or final subscription.



          Finalise is a special pipe, it does the same than Tap but after subscription, it is good for example to log final results or to cancel subscription after it completes.



          CatchError is a pipe which alters the result but it is only called if the previous step has thrown an error. This is used to avoid unhandled error and you should return an observable "default" instead of the failed observable (so we handle the error and the app does not break).



          You can guess when your observable had an Error if catchError has been launched and handle it straight away before it reach the subscription.



          If this pipe is not launched, the result is considered without error.






          share|improve this answer






















          • This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
            – JoniJnm
            Oct 29 at 17:28






          • 1




            The order of execution is Observable -> Pipes in order (except finalize) -> subscribe -> finalize Pipe with this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
            – Ignacio Bustos
            Nov 11 at 16:02










          • So something like this: stackblitz.com/edit/typescript-kpusau
            – JoniJnm
            Nov 22 at 8:54














          up vote
          1
          down vote



          accepted










          According to https://www.learnrxjs.io/



          With the latest RXJS you can use this 3 operators



          const obs = getMyObservable().pipe( // Let's assume 'obs' returns an array
          tap(() => console.log('Action performed before any other')),
          catchError(() => console.error('Error emitted'); return of(); ), // We return instead
          finalize(() => console.log('Action to be executed always')) // Either Error or Success
          );
          obs.subscribe(data => console.log(data)); // After everything, we log the output.


          Hope it helps



          EDIT based on JoniJnm comment



          To be More specific, there are, 3 main Pipes:



          1. Pipes that does alter the result before subscription.

          2. Pipes that does not alter the result before subscription.

          3. Special Pipes.

          Tap for example is from the second type, it can takes the input from the observable or previous pipes and do anything with it but cannot change the result of the pipe for the next step.



          Map is similar but it belongs to the first type of Pipe, it takes an input and has to return an output that can be used in the next pipe or final subscription.



          Finalise is a special pipe, it does the same than Tap but after subscription, it is good for example to log final results or to cancel subscription after it completes.



          CatchError is a pipe which alters the result but it is only called if the previous step has thrown an error. This is used to avoid unhandled error and you should return an observable "default" instead of the failed observable (so we handle the error and the app does not break).



          You can guess when your observable had an Error if catchError has been launched and handle it straight away before it reach the subscription.



          If this pipe is not launched, the result is considered without error.






          share|improve this answer






















          • This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
            – JoniJnm
            Oct 29 at 17:28






          • 1




            The order of execution is Observable -> Pipes in order (except finalize) -> subscribe -> finalize Pipe with this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
            – Ignacio Bustos
            Nov 11 at 16:02










          • So something like this: stackblitz.com/edit/typescript-kpusau
            – JoniJnm
            Nov 22 at 8:54












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          According to https://www.learnrxjs.io/



          With the latest RXJS you can use this 3 operators



          const obs = getMyObservable().pipe( // Let's assume 'obs' returns an array
          tap(() => console.log('Action performed before any other')),
          catchError(() => console.error('Error emitted'); return of(); ), // We return instead
          finalize(() => console.log('Action to be executed always')) // Either Error or Success
          );
          obs.subscribe(data => console.log(data)); // After everything, we log the output.


          Hope it helps



          EDIT based on JoniJnm comment



          To be More specific, there are, 3 main Pipes:



          1. Pipes that does alter the result before subscription.

          2. Pipes that does not alter the result before subscription.

          3. Special Pipes.

          Tap for example is from the second type, it can takes the input from the observable or previous pipes and do anything with it but cannot change the result of the pipe for the next step.



          Map is similar but it belongs to the first type of Pipe, it takes an input and has to return an output that can be used in the next pipe or final subscription.



          Finalise is a special pipe, it does the same than Tap but after subscription, it is good for example to log final results or to cancel subscription after it completes.



          CatchError is a pipe which alters the result but it is only called if the previous step has thrown an error. This is used to avoid unhandled error and you should return an observable "default" instead of the failed observable (so we handle the error and the app does not break).



          You can guess when your observable had an Error if catchError has been launched and handle it straight away before it reach the subscription.



          If this pipe is not launched, the result is considered without error.






          share|improve this answer














          According to https://www.learnrxjs.io/



          With the latest RXJS you can use this 3 operators



          const obs = getMyObservable().pipe( // Let's assume 'obs' returns an array
          tap(() => console.log('Action performed before any other')),
          catchError(() => console.error('Error emitted'); return of(); ), // We return instead
          finalize(() => console.log('Action to be executed always')) // Either Error or Success
          );
          obs.subscribe(data => console.log(data)); // After everything, we log the output.


          Hope it helps



          EDIT based on JoniJnm comment



          To be More specific, there are, 3 main Pipes:



          1. Pipes that does alter the result before subscription.

          2. Pipes that does not alter the result before subscription.

          3. Special Pipes.

          Tap for example is from the second type, it can takes the input from the observable or previous pipes and do anything with it but cannot change the result of the pipe for the next step.



          Map is similar but it belongs to the first type of Pipe, it takes an input and has to return an output that can be used in the next pipe or final subscription.



          Finalise is a special pipe, it does the same than Tap but after subscription, it is good for example to log final results or to cancel subscription after it completes.



          CatchError is a pipe which alters the result but it is only called if the previous step has thrown an error. This is used to avoid unhandled error and you should return an observable "default" instead of the failed observable (so we handle the error and the app does not break).



          You can guess when your observable had an Error if catchError has been launched and handle it straight away before it reach the subscription.



          If this pipe is not launched, the result is considered without error.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 16:17

























          answered Jun 27 at 17:52









          Ignacio Bustos

          4792719




          4792719











          • This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
            – JoniJnm
            Oct 29 at 17:28






          • 1




            The order of execution is Observable -> Pipes in order (except finalize) -> subscribe -> finalize Pipe with this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
            – Ignacio Bustos
            Nov 11 at 16:02










          • So something like this: stackblitz.com/edit/typescript-kpusau
            – JoniJnm
            Nov 22 at 8:54
















          • This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
            – JoniJnm
            Oct 29 at 17:28






          • 1




            The order of execution is Observable -> Pipes in order (except finalize) -> subscribe -> finalize Pipe with this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
            – Ignacio Bustos
            Nov 11 at 16:02










          • So something like this: stackblitz.com/edit/typescript-kpusau
            – JoniJnm
            Nov 22 at 8:54















          This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
          – JoniJnm
          Oct 29 at 17:28




          This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
          – JoniJnm
          Oct 29 at 17:28




          1




          1




          The order of execution is Observable -> Pipes in order (except finalize) -> subscribe -> finalize Pipe with this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
          – Ignacio Bustos
          Nov 11 at 16:02




          The order of execution is Observable -> Pipes in order (except finalize) -> subscribe -> finalize Pipe with this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
          – Ignacio Bustos
          Nov 11 at 16:02












          So something like this: stackblitz.com/edit/typescript-kpusau
          – JoniJnm
          Nov 22 at 8:54




          So something like this: stackblitz.com/edit/typescript-kpusau
          – JoniJnm
          Nov 22 at 8:54

















          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%2f50409658%2fhow-can-i-know-if-observable-has-finalized-with-error-or-without-error%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







          這個網誌中的熱門文章

          What does pagestruct do in Eviews?

          Dutch intervention in Lombok and Karangasem

          Channel Islands