TSLint - Promises must be handled appropriately with `finally`












1















I'm getting this error from TSLint and I'm trying to understand why it is complaining about.



I have a function which invokes another method which returns a promise but the first function does not return the promise because it just waits for it to finish and update an internal state.



I've simplified it to this function and just use Q() to simulate an invocation that returns a promise.



export function DoSomethingAsync(): void {
Q().then(r => {
console.log('test');
}).catch(err => {
log.error("wow");
}).finally(() => {
log.info("at finally")
});
}


When I run tslint on my project now I'm getting the following error:



ERROR: C:/dev/local_cache_service.ts[31, 5]: Promises must be handled appropriately



If I remove the finally call tslint passes without errors.



export function DoSomethingAsync(): void {
Q().then(r => {
console.log('test');
}).catch(err => {
log.error("wow");
});
}


When I create the same function on a seed typescript project this behavior does not reproduce...










share|improve this question





























    1















    I'm getting this error from TSLint and I'm trying to understand why it is complaining about.



    I have a function which invokes another method which returns a promise but the first function does not return the promise because it just waits for it to finish and update an internal state.



    I've simplified it to this function and just use Q() to simulate an invocation that returns a promise.



    export function DoSomethingAsync(): void {
    Q().then(r => {
    console.log('test');
    }).catch(err => {
    log.error("wow");
    }).finally(() => {
    log.info("at finally")
    });
    }


    When I run tslint on my project now I'm getting the following error:



    ERROR: C:/dev/local_cache_service.ts[31, 5]: Promises must be handled appropriately



    If I remove the finally call tslint passes without errors.



    export function DoSomethingAsync(): void {
    Q().then(r => {
    console.log('test');
    }).catch(err => {
    log.error("wow");
    });
    }


    When I create the same function on a seed typescript project this behavior does not reproduce...










    share|improve this question



























      1












      1








      1








      I'm getting this error from TSLint and I'm trying to understand why it is complaining about.



      I have a function which invokes another method which returns a promise but the first function does not return the promise because it just waits for it to finish and update an internal state.



      I've simplified it to this function and just use Q() to simulate an invocation that returns a promise.



      export function DoSomethingAsync(): void {
      Q().then(r => {
      console.log('test');
      }).catch(err => {
      log.error("wow");
      }).finally(() => {
      log.info("at finally")
      });
      }


      When I run tslint on my project now I'm getting the following error:



      ERROR: C:/dev/local_cache_service.ts[31, 5]: Promises must be handled appropriately



      If I remove the finally call tslint passes without errors.



      export function DoSomethingAsync(): void {
      Q().then(r => {
      console.log('test');
      }).catch(err => {
      log.error("wow");
      });
      }


      When I create the same function on a seed typescript project this behavior does not reproduce...










      share|improve this question
















      I'm getting this error from TSLint and I'm trying to understand why it is complaining about.



      I have a function which invokes another method which returns a promise but the first function does not return the promise because it just waits for it to finish and update an internal state.



      I've simplified it to this function and just use Q() to simulate an invocation that returns a promise.



      export function DoSomethingAsync(): void {
      Q().then(r => {
      console.log('test');
      }).catch(err => {
      log.error("wow");
      }).finally(() => {
      log.info("at finally")
      });
      }


      When I run tslint on my project now I'm getting the following error:



      ERROR: C:/dev/local_cache_service.ts[31, 5]: Promises must be handled appropriately



      If I remove the finally call tslint passes without errors.



      export function DoSomethingAsync(): void {
      Q().then(r => {
      console.log('test');
      }).catch(err => {
      log.error("wow");
      });
      }


      When I create the same function on a seed typescript project this behavior does not reproduce...







      typescript q tslint






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 8:01







      Ido Ran

















      asked Nov 26 '18 at 6:48









      Ido RanIdo Ran

      4,346851103




      4,346851103
























          2 Answers
          2






          active

          oldest

          votes


















          4














          This is a complaint from the no-floating-promises rule. As per its description:




          Creating a Promise and not storing or returning it may let other code run independently of its result. This can cause unexpected and/or non-deterministic behavior depending on external timing factors.



          It’s typically better to return Promises from functions that start them, then handle them in calling code.



          Use no-unused-expression in addition to this rule to reveal even more floating promises.




          Specifically in your case, it's because you're running code in a .finally block after the .catch block. That's considered dangerous by the rule because if the code within the .finally throws an error, it'll be unhandled by the calling code.



          The best thing for you to do would be to return the promise, so the function's return type is a Promise/Q instead of void.



          Tip: you can run tslint -t stylish or tslint -t verbose to see rule names along with their complaints!






          share|improve this answer
























          • Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.

            – Ido Ran
            Nov 27 '18 at 8:30











          • In that case, two quick options: * Put a // tslint:disable-next-line:no-floating-promises above the line being complained about * Put a .catch after the .finally, which will handle the case of the .finally code throwing an error You could also consider having something that receives these floating promises and .catches them. safePromiseCatcher(Q().then(...));.

            – Josh
            Nov 27 '18 at 19:51





















          0














          The solution was to add a call to .done() at the end of the promise call chain.



          From what I understood done converts any unhandled exceptions to a regular unhandled exception.






          share|improve this answer























            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%2f53475974%2ftslint-promises-must-be-handled-appropriately-with-finally%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









            4














            This is a complaint from the no-floating-promises rule. As per its description:




            Creating a Promise and not storing or returning it may let other code run independently of its result. This can cause unexpected and/or non-deterministic behavior depending on external timing factors.



            It’s typically better to return Promises from functions that start them, then handle them in calling code.



            Use no-unused-expression in addition to this rule to reveal even more floating promises.




            Specifically in your case, it's because you're running code in a .finally block after the .catch block. That's considered dangerous by the rule because if the code within the .finally throws an error, it'll be unhandled by the calling code.



            The best thing for you to do would be to return the promise, so the function's return type is a Promise/Q instead of void.



            Tip: you can run tslint -t stylish or tslint -t verbose to see rule names along with their complaints!






            share|improve this answer
























            • Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.

              – Ido Ran
              Nov 27 '18 at 8:30











            • In that case, two quick options: * Put a // tslint:disable-next-line:no-floating-promises above the line being complained about * Put a .catch after the .finally, which will handle the case of the .finally code throwing an error You could also consider having something that receives these floating promises and .catches them. safePromiseCatcher(Q().then(...));.

              – Josh
              Nov 27 '18 at 19:51


















            4














            This is a complaint from the no-floating-promises rule. As per its description:




            Creating a Promise and not storing or returning it may let other code run independently of its result. This can cause unexpected and/or non-deterministic behavior depending on external timing factors.



            It’s typically better to return Promises from functions that start them, then handle them in calling code.



            Use no-unused-expression in addition to this rule to reveal even more floating promises.




            Specifically in your case, it's because you're running code in a .finally block after the .catch block. That's considered dangerous by the rule because if the code within the .finally throws an error, it'll be unhandled by the calling code.



            The best thing for you to do would be to return the promise, so the function's return type is a Promise/Q instead of void.



            Tip: you can run tslint -t stylish or tslint -t verbose to see rule names along with their complaints!






            share|improve this answer
























            • Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.

              – Ido Ran
              Nov 27 '18 at 8:30











            • In that case, two quick options: * Put a // tslint:disable-next-line:no-floating-promises above the line being complained about * Put a .catch after the .finally, which will handle the case of the .finally code throwing an error You could also consider having something that receives these floating promises and .catches them. safePromiseCatcher(Q().then(...));.

              – Josh
              Nov 27 '18 at 19:51
















            4












            4








            4







            This is a complaint from the no-floating-promises rule. As per its description:




            Creating a Promise and not storing or returning it may let other code run independently of its result. This can cause unexpected and/or non-deterministic behavior depending on external timing factors.



            It’s typically better to return Promises from functions that start them, then handle them in calling code.



            Use no-unused-expression in addition to this rule to reveal even more floating promises.




            Specifically in your case, it's because you're running code in a .finally block after the .catch block. That's considered dangerous by the rule because if the code within the .finally throws an error, it'll be unhandled by the calling code.



            The best thing for you to do would be to return the promise, so the function's return type is a Promise/Q instead of void.



            Tip: you can run tslint -t stylish or tslint -t verbose to see rule names along with their complaints!






            share|improve this answer













            This is a complaint from the no-floating-promises rule. As per its description:




            Creating a Promise and not storing or returning it may let other code run independently of its result. This can cause unexpected and/or non-deterministic behavior depending on external timing factors.



            It’s typically better to return Promises from functions that start them, then handle them in calling code.



            Use no-unused-expression in addition to this rule to reveal even more floating promises.




            Specifically in your case, it's because you're running code in a .finally block after the .catch block. That's considered dangerous by the rule because if the code within the .finally throws an error, it'll be unhandled by the calling code.



            The best thing for you to do would be to return the promise, so the function's return type is a Promise/Q instead of void.



            Tip: you can run tslint -t stylish or tslint -t verbose to see rule names along with their complaints!







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 27 '18 at 4:23









            JoshJosh

            821512




            821512













            • Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.

              – Ido Ran
              Nov 27 '18 at 8:30











            • In that case, two quick options: * Put a // tslint:disable-next-line:no-floating-promises above the line being complained about * Put a .catch after the .finally, which will handle the case of the .finally code throwing an error You could also consider having something that receives these floating promises and .catches them. safePromiseCatcher(Q().then(...));.

              – Josh
              Nov 27 '18 at 19:51





















            • Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.

              – Ido Ran
              Nov 27 '18 at 8:30











            • In that case, two quick options: * Put a // tslint:disable-next-line:no-floating-promises above the line being complained about * Put a .catch after the .finally, which will handle the case of the .finally code throwing an error You could also consider having something that receives these floating promises and .catches them. safePromiseCatcher(Q().then(...));.

              – Josh
              Nov 27 '18 at 19:51



















            Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.

            – Ido Ran
            Nov 27 '18 at 8:30





            Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.

            – Ido Ran
            Nov 27 '18 at 8:30













            In that case, two quick options: * Put a // tslint:disable-next-line:no-floating-promises above the line being complained about * Put a .catch after the .finally, which will handle the case of the .finally code throwing an error You could also consider having something that receives these floating promises and .catches them. safePromiseCatcher(Q().then(...));.

            – Josh
            Nov 27 '18 at 19:51







            In that case, two quick options: * Put a // tslint:disable-next-line:no-floating-promises above the line being complained about * Put a .catch after the .finally, which will handle the case of the .finally code throwing an error You could also consider having something that receives these floating promises and .catches them. safePromiseCatcher(Q().then(...));.

            – Josh
            Nov 27 '18 at 19:51















            0














            The solution was to add a call to .done() at the end of the promise call chain.



            From what I understood done converts any unhandled exceptions to a regular unhandled exception.






            share|improve this answer




























              0














              The solution was to add a call to .done() at the end of the promise call chain.



              From what I understood done converts any unhandled exceptions to a regular unhandled exception.






              share|improve this answer


























                0












                0








                0







                The solution was to add a call to .done() at the end of the promise call chain.



                From what I understood done converts any unhandled exceptions to a regular unhandled exception.






                share|improve this answer













                The solution was to add a call to .done() at the end of the promise call chain.



                From what I understood done converts any unhandled exceptions to a regular unhandled exception.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 14:35









                Ido RanIdo Ran

                4,346851103




                4,346851103






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475974%2ftslint-promises-must-be-handled-appropriately-with-finally%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

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks