Promise.all.then Not all code paths return a value












-1















I have the following function that I want to return Promise< number >



async fetchCommentLines(commitDict: CommitDict): Promise < number > {
if (GitCommentService.isLoggedIn()) {
const commentLines = Object.values(commitDict).map(async commit => {
// ...
// do the job and return number
// ...
return lineNums;
});

Promise.all(commentLines)
.then(commitLines => {
return Array.prototype.concat.apply(, commitLines);
});
} else {
return as number;
}
}


Firstly I got "function lacks ending return statement and return type does not include 'undefined'"



And then I added undefined (so return type becomes Promise< number | undefined >)



But I'm getting "not all code paths return a value" this time.



It seems I'm not considering a possible code path with below code



Promise.all(...)
.then(val => {return ...})


What I'm missing?



I also tried this



Promise.all(...)
.then(val => {return ...})
.catch(e => {return ...})


But it was not helpful



Note: My main purpose is to return Promise< number >, not Promise< number | undefined >










share|improve this question





























    -1















    I have the following function that I want to return Promise< number >



    async fetchCommentLines(commitDict: CommitDict): Promise < number > {
    if (GitCommentService.isLoggedIn()) {
    const commentLines = Object.values(commitDict).map(async commit => {
    // ...
    // do the job and return number
    // ...
    return lineNums;
    });

    Promise.all(commentLines)
    .then(commitLines => {
    return Array.prototype.concat.apply(, commitLines);
    });
    } else {
    return as number;
    }
    }


    Firstly I got "function lacks ending return statement and return type does not include 'undefined'"



    And then I added undefined (so return type becomes Promise< number | undefined >)



    But I'm getting "not all code paths return a value" this time.



    It seems I'm not considering a possible code path with below code



    Promise.all(...)
    .then(val => {return ...})


    What I'm missing?



    I also tried this



    Promise.all(...)
    .then(val => {return ...})
    .catch(e => {return ...})


    But it was not helpful



    Note: My main purpose is to return Promise< number >, not Promise< number | undefined >










    share|improve this question



























      -1












      -1








      -1








      I have the following function that I want to return Promise< number >



      async fetchCommentLines(commitDict: CommitDict): Promise < number > {
      if (GitCommentService.isLoggedIn()) {
      const commentLines = Object.values(commitDict).map(async commit => {
      // ...
      // do the job and return number
      // ...
      return lineNums;
      });

      Promise.all(commentLines)
      .then(commitLines => {
      return Array.prototype.concat.apply(, commitLines);
      });
      } else {
      return as number;
      }
      }


      Firstly I got "function lacks ending return statement and return type does not include 'undefined'"



      And then I added undefined (so return type becomes Promise< number | undefined >)



      But I'm getting "not all code paths return a value" this time.



      It seems I'm not considering a possible code path with below code



      Promise.all(...)
      .then(val => {return ...})


      What I'm missing?



      I also tried this



      Promise.all(...)
      .then(val => {return ...})
      .catch(e => {return ...})


      But it was not helpful



      Note: My main purpose is to return Promise< number >, not Promise< number | undefined >










      share|improve this question
















      I have the following function that I want to return Promise< number >



      async fetchCommentLines(commitDict: CommitDict): Promise < number > {
      if (GitCommentService.isLoggedIn()) {
      const commentLines = Object.values(commitDict).map(async commit => {
      // ...
      // do the job and return number
      // ...
      return lineNums;
      });

      Promise.all(commentLines)
      .then(commitLines => {
      return Array.prototype.concat.apply(, commitLines);
      });
      } else {
      return as number;
      }
      }


      Firstly I got "function lacks ending return statement and return type does not include 'undefined'"



      And then I added undefined (so return type becomes Promise< number | undefined >)



      But I'm getting "not all code paths return a value" this time.



      It seems I'm not considering a possible code path with below code



      Promise.all(...)
      .then(val => {return ...})


      What I'm missing?



      I also tried this



      Promise.all(...)
      .then(val => {return ...})
      .catch(e => {return ...})


      But it was not helpful



      Note: My main purpose is to return Promise< number >, not Promise< number | undefined >







      javascript typescript promise async-await es6-promise






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 29 '18 at 13:33







      user3790180

















      asked Dec 29 '18 at 13:26









      user3790180user3790180

      10819




      10819
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Your branch with Promise.all never issues a return (value) statement. The then callback does, but not the code outside the then callback.



          You probably want to return the result of Promise.all().then().



              return Promise.all(commentLines)
          // ^^^^^^
          .then(commitLines => {
          return Array.prototype.concat.apply(, commitLines);
          });





          share|improve this answer





















          • 1





            My purpose with it was to flatten a number into number So [[1,2],[3,4]] into [1,2,3,4] I'll try the other ways you mentioned

            – user3790180
            Dec 29 '18 at 13:46













          • @user3790180 - Ah! In that case, ignore me. :-) (I've removed that part of the answer.) You might look into Array.prototype.flat. It's at Stage 3 and already supported by Chrome and Firefox, easily polyfilled for others... :-) That would be return Promise.all(commentLines).then(commitLines => commitLines.flat());

            – T.J. Crowder
            Dec 29 '18 at 13:55





















          2














          you should return the Promise



          return Promise.all(commentLines).then(...)
          //....


          or wait for the promise and return the result object



          let lines = await Promise.all(commentLines)
          return .concat(...lines)





          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%2f53969982%2fpromise-all-then-not-all-code-paths-return-a-value%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









            1














            Your branch with Promise.all never issues a return (value) statement. The then callback does, but not the code outside the then callback.



            You probably want to return the result of Promise.all().then().



                return Promise.all(commentLines)
            // ^^^^^^
            .then(commitLines => {
            return Array.prototype.concat.apply(, commitLines);
            });





            share|improve this answer





















            • 1





              My purpose with it was to flatten a number into number So [[1,2],[3,4]] into [1,2,3,4] I'll try the other ways you mentioned

              – user3790180
              Dec 29 '18 at 13:46













            • @user3790180 - Ah! In that case, ignore me. :-) (I've removed that part of the answer.) You might look into Array.prototype.flat. It's at Stage 3 and already supported by Chrome and Firefox, easily polyfilled for others... :-) That would be return Promise.all(commentLines).then(commitLines => commitLines.flat());

              – T.J. Crowder
              Dec 29 '18 at 13:55


















            1














            Your branch with Promise.all never issues a return (value) statement. The then callback does, but not the code outside the then callback.



            You probably want to return the result of Promise.all().then().



                return Promise.all(commentLines)
            // ^^^^^^
            .then(commitLines => {
            return Array.prototype.concat.apply(, commitLines);
            });





            share|improve this answer





















            • 1





              My purpose with it was to flatten a number into number So [[1,2],[3,4]] into [1,2,3,4] I'll try the other ways you mentioned

              – user3790180
              Dec 29 '18 at 13:46













            • @user3790180 - Ah! In that case, ignore me. :-) (I've removed that part of the answer.) You might look into Array.prototype.flat. It's at Stage 3 and already supported by Chrome and Firefox, easily polyfilled for others... :-) That would be return Promise.all(commentLines).then(commitLines => commitLines.flat());

              – T.J. Crowder
              Dec 29 '18 at 13:55
















            1












            1








            1







            Your branch with Promise.all never issues a return (value) statement. The then callback does, but not the code outside the then callback.



            You probably want to return the result of Promise.all().then().



                return Promise.all(commentLines)
            // ^^^^^^
            .then(commitLines => {
            return Array.prototype.concat.apply(, commitLines);
            });





            share|improve this answer















            Your branch with Promise.all never issues a return (value) statement. The then callback does, but not the code outside the then callback.



            You probably want to return the result of Promise.all().then().



                return Promise.all(commentLines)
            // ^^^^^^
            .then(commitLines => {
            return Array.prototype.concat.apply(, commitLines);
            });






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 29 '18 at 13:56

























            answered Dec 29 '18 at 13:29









            T.J. CrowderT.J. Crowder

            682k12112081305




            682k12112081305








            • 1





              My purpose with it was to flatten a number into number So [[1,2],[3,4]] into [1,2,3,4] I'll try the other ways you mentioned

              – user3790180
              Dec 29 '18 at 13:46













            • @user3790180 - Ah! In that case, ignore me. :-) (I've removed that part of the answer.) You might look into Array.prototype.flat. It's at Stage 3 and already supported by Chrome and Firefox, easily polyfilled for others... :-) That would be return Promise.all(commentLines).then(commitLines => commitLines.flat());

              – T.J. Crowder
              Dec 29 '18 at 13:55
















            • 1





              My purpose with it was to flatten a number into number So [[1,2],[3,4]] into [1,2,3,4] I'll try the other ways you mentioned

              – user3790180
              Dec 29 '18 at 13:46













            • @user3790180 - Ah! In that case, ignore me. :-) (I've removed that part of the answer.) You might look into Array.prototype.flat. It's at Stage 3 and already supported by Chrome and Firefox, easily polyfilled for others... :-) That would be return Promise.all(commentLines).then(commitLines => commitLines.flat());

              – T.J. Crowder
              Dec 29 '18 at 13:55










            1




            1





            My purpose with it was to flatten a number into number So [[1,2],[3,4]] into [1,2,3,4] I'll try the other ways you mentioned

            – user3790180
            Dec 29 '18 at 13:46







            My purpose with it was to flatten a number into number So [[1,2],[3,4]] into [1,2,3,4] I'll try the other ways you mentioned

            – user3790180
            Dec 29 '18 at 13:46















            @user3790180 - Ah! In that case, ignore me. :-) (I've removed that part of the answer.) You might look into Array.prototype.flat. It's at Stage 3 and already supported by Chrome and Firefox, easily polyfilled for others... :-) That would be return Promise.all(commentLines).then(commitLines => commitLines.flat());

            – T.J. Crowder
            Dec 29 '18 at 13:55







            @user3790180 - Ah! In that case, ignore me. :-) (I've removed that part of the answer.) You might look into Array.prototype.flat. It's at Stage 3 and already supported by Chrome and Firefox, easily polyfilled for others... :-) That would be return Promise.all(commentLines).then(commitLines => commitLines.flat());

            – T.J. Crowder
            Dec 29 '18 at 13:55















            2














            you should return the Promise



            return Promise.all(commentLines).then(...)
            //....


            or wait for the promise and return the result object



            let lines = await Promise.all(commentLines)
            return .concat(...lines)





            share|improve this answer






























              2














              you should return the Promise



              return Promise.all(commentLines).then(...)
              //....


              or wait for the promise and return the result object



              let lines = await Promise.all(commentLines)
              return .concat(...lines)





              share|improve this answer




























                2












                2








                2







                you should return the Promise



                return Promise.all(commentLines).then(...)
                //....


                or wait for the promise and return the result object



                let lines = await Promise.all(commentLines)
                return .concat(...lines)





                share|improve this answer















                you should return the Promise



                return Promise.all(commentLines).then(...)
                //....


                or wait for the promise and return the result object



                let lines = await Promise.all(commentLines)
                return .concat(...lines)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 29 '18 at 13:41









                Bergi

                367k58546873




                367k58546873










                answered Dec 29 '18 at 13:29









                apple appleapple apple

                2,5401723




                2,5401723






























                    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%2f53969982%2fpromise-all-then-not-all-code-paths-return-a-value%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

                    Monofisismo

                    compose and upload a new article using a custom form

                    How to correct the classpath of spring boot application so that it contains a single, compatible version of...