Unable to retrieve response in promise.all












0















I am making a parallel call like below.



let parllel_call: [Promise<any>, Promise<any>] = [
Function1(xxxxReq),
Function2(yyyyReq),
];
let parllel_call_result = await Promise.all(parllel_call);
console.log('parllel_call_result ', JSON.stringify(parllel_call_result));
let xxxxRes = parllel_call_result[0];
console.log('xxxxRes.status '+xxxxRes.status+' message '+xxxxRes.message+' message '+xxxxRes.data.message);
let yyyyRes = parllel_call_result[1];
console.log('yyyyRes.status '+yyyyRes.status+' message '+yyyyRes.message+' message '+yyyyRes.data.message);


Below is Function1.



async function Function1(xxxxReq) {
console.log("Start - Function1");

await axios
.post(some_url, axxxxReq)
.then(res => {

console.log('response '+res.status+' message '+res.message);
console.log("End - Function1");
return res;
})
.catch(error => {
console.error("error " + error);
console.log("End - Function1");
return error;
});
}


Below is Function2.



async function Function2(yyyyReq) {
console.log("Start - Function2");

await axios
.post(some_other_url, yyyyReq)
.then(res => {
console.log('response '+res.status+' message '+res.data.message);
console.log("End - Function2");
return res;
})
.catch(error => {
console.error("error " + error);
console.log("End - Function2");
return error;
});
}


I am able to invoke Function 1 & 2 parallelly and able to get the response within the functions.
But when the promises are resolved and responses are gathered in the order of how input is given, the response is not being collected from promises.
Log says null is passed into Promise.all response array.



Below is the log of execution.



info: Doing parllel call
Start - Function1
Start - Function2
info: response 200 message undefined
info: End - Function1
info: response 200 message undefined
info: End - Function2
parllel_call_result [null,null]
error endAssesment TypeError: Cannot read property 'status' of undefined
info: Execution took 14323 ms, user function completed successfully


What I am missing here.










share|improve this question



























    0















    I am making a parallel call like below.



    let parllel_call: [Promise<any>, Promise<any>] = [
    Function1(xxxxReq),
    Function2(yyyyReq),
    ];
    let parllel_call_result = await Promise.all(parllel_call);
    console.log('parllel_call_result ', JSON.stringify(parllel_call_result));
    let xxxxRes = parllel_call_result[0];
    console.log('xxxxRes.status '+xxxxRes.status+' message '+xxxxRes.message+' message '+xxxxRes.data.message);
    let yyyyRes = parllel_call_result[1];
    console.log('yyyyRes.status '+yyyyRes.status+' message '+yyyyRes.message+' message '+yyyyRes.data.message);


    Below is Function1.



    async function Function1(xxxxReq) {
    console.log("Start - Function1");

    await axios
    .post(some_url, axxxxReq)
    .then(res => {

    console.log('response '+res.status+' message '+res.message);
    console.log("End - Function1");
    return res;
    })
    .catch(error => {
    console.error("error " + error);
    console.log("End - Function1");
    return error;
    });
    }


    Below is Function2.



    async function Function2(yyyyReq) {
    console.log("Start - Function2");

    await axios
    .post(some_other_url, yyyyReq)
    .then(res => {
    console.log('response '+res.status+' message '+res.data.message);
    console.log("End - Function2");
    return res;
    })
    .catch(error => {
    console.error("error " + error);
    console.log("End - Function2");
    return error;
    });
    }


    I am able to invoke Function 1 & 2 parallelly and able to get the response within the functions.
    But when the promises are resolved and responses are gathered in the order of how input is given, the response is not being collected from promises.
    Log says null is passed into Promise.all response array.



    Below is the log of execution.



    info: Doing parllel call
    Start - Function1
    Start - Function2
    info: response 200 message undefined
    info: End - Function1
    info: response 200 message undefined
    info: End - Function2
    parllel_call_result [null,null]
    error endAssesment TypeError: Cannot read property 'status' of undefined
    info: Execution took 14323 ms, user function completed successfully


    What I am missing here.










    share|improve this question

























      0












      0








      0








      I am making a parallel call like below.



      let parllel_call: [Promise<any>, Promise<any>] = [
      Function1(xxxxReq),
      Function2(yyyyReq),
      ];
      let parllel_call_result = await Promise.all(parllel_call);
      console.log('parllel_call_result ', JSON.stringify(parllel_call_result));
      let xxxxRes = parllel_call_result[0];
      console.log('xxxxRes.status '+xxxxRes.status+' message '+xxxxRes.message+' message '+xxxxRes.data.message);
      let yyyyRes = parllel_call_result[1];
      console.log('yyyyRes.status '+yyyyRes.status+' message '+yyyyRes.message+' message '+yyyyRes.data.message);


      Below is Function1.



      async function Function1(xxxxReq) {
      console.log("Start - Function1");

      await axios
      .post(some_url, axxxxReq)
      .then(res => {

      console.log('response '+res.status+' message '+res.message);
      console.log("End - Function1");
      return res;
      })
      .catch(error => {
      console.error("error " + error);
      console.log("End - Function1");
      return error;
      });
      }


      Below is Function2.



      async function Function2(yyyyReq) {
      console.log("Start - Function2");

      await axios
      .post(some_other_url, yyyyReq)
      .then(res => {
      console.log('response '+res.status+' message '+res.data.message);
      console.log("End - Function2");
      return res;
      })
      .catch(error => {
      console.error("error " + error);
      console.log("End - Function2");
      return error;
      });
      }


      I am able to invoke Function 1 & 2 parallelly and able to get the response within the functions.
      But when the promises are resolved and responses are gathered in the order of how input is given, the response is not being collected from promises.
      Log says null is passed into Promise.all response array.



      Below is the log of execution.



      info: Doing parllel call
      Start - Function1
      Start - Function2
      info: response 200 message undefined
      info: End - Function1
      info: response 200 message undefined
      info: End - Function2
      parllel_call_result [null,null]
      error endAssesment TypeError: Cannot read property 'status' of undefined
      info: Execution took 14323 ms, user function completed successfully


      What I am missing here.










      share|improve this question














      I am making a parallel call like below.



      let parllel_call: [Promise<any>, Promise<any>] = [
      Function1(xxxxReq),
      Function2(yyyyReq),
      ];
      let parllel_call_result = await Promise.all(parllel_call);
      console.log('parllel_call_result ', JSON.stringify(parllel_call_result));
      let xxxxRes = parllel_call_result[0];
      console.log('xxxxRes.status '+xxxxRes.status+' message '+xxxxRes.message+' message '+xxxxRes.data.message);
      let yyyyRes = parllel_call_result[1];
      console.log('yyyyRes.status '+yyyyRes.status+' message '+yyyyRes.message+' message '+yyyyRes.data.message);


      Below is Function1.



      async function Function1(xxxxReq) {
      console.log("Start - Function1");

      await axios
      .post(some_url, axxxxReq)
      .then(res => {

      console.log('response '+res.status+' message '+res.message);
      console.log("End - Function1");
      return res;
      })
      .catch(error => {
      console.error("error " + error);
      console.log("End - Function1");
      return error;
      });
      }


      Below is Function2.



      async function Function2(yyyyReq) {
      console.log("Start - Function2");

      await axios
      .post(some_other_url, yyyyReq)
      .then(res => {
      console.log('response '+res.status+' message '+res.data.message);
      console.log("End - Function2");
      return res;
      })
      .catch(error => {
      console.error("error " + error);
      console.log("End - Function2");
      return error;
      });
      }


      I am able to invoke Function 1 & 2 parallelly and able to get the response within the functions.
      But when the promises are resolved and responses are gathered in the order of how input is given, the response is not being collected from promises.
      Log says null is passed into Promise.all response array.



      Below is the log of execution.



      info: Doing parllel call
      Start - Function1
      Start - Function2
      info: response 200 message undefined
      info: End - Function1
      info: response 200 message undefined
      info: End - Function2
      parllel_call_result [null,null]
      error endAssesment TypeError: Cannot read property 'status' of undefined
      info: Execution took 14323 ms, user function completed successfully


      What I am missing here.







      node.js typescript firebase google-cloud-functions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 19:49









      kpvsrkpkpvsrkp

      93110




      93110
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Your async functions don't return any value, therefore the promise they return will have an undefined resolved value. The return statements you do have are inside .then() or .catch() callbacks. You don't have any return at the top level that actually sets the resolved value of the async function. You need to return the value you get from the await or get rid of the await and just return the promise.



          function Function2(yyyyReq) {
          console.log("Start - Function2");

          return axios
          .post(some_other_url, yyyyReq)
          .then(res => {
          console.log('response '+res.status+' message '+res.data.message);
          console.log("End - Function2");
          return res;
          })
          .catch(error => {
          console.error("error " + error);
          console.log("End - Function2");
          throw error;
          });
          }


          And, once you remove the await and just return the promise from the axios() call, you don't need your function to be async any more either.



          In addition, if you want your .catch() to still reject the promise, then you need to rethrow the error.





          If you wanted to keep the async/await, you would have to capture and return the value.



          async function Function2(yyyyReq) {
          console.log("Start - Function2");

          let val = await axios
          .post(some_other_url, yyyyReq)
          .then(res => {
          console.log('response '+res.status+' message '+res.data.message);
          console.log("End - Function2");
          return res;
          })
          .catch(error => {
          console.error("error " + error);
          console.log("End - Function2");
          throw error;
          });
          return val;
          }





          share|improve this answer


























          • Also added any as the return type for functions.

            – kpvsrkp
            Nov 25 '18 at 13:34











          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%2f53461806%2funable-to-retrieve-response-in-promise-all%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









          1














          Your async functions don't return any value, therefore the promise they return will have an undefined resolved value. The return statements you do have are inside .then() or .catch() callbacks. You don't have any return at the top level that actually sets the resolved value of the async function. You need to return the value you get from the await or get rid of the await and just return the promise.



          function Function2(yyyyReq) {
          console.log("Start - Function2");

          return axios
          .post(some_other_url, yyyyReq)
          .then(res => {
          console.log('response '+res.status+' message '+res.data.message);
          console.log("End - Function2");
          return res;
          })
          .catch(error => {
          console.error("error " + error);
          console.log("End - Function2");
          throw error;
          });
          }


          And, once you remove the await and just return the promise from the axios() call, you don't need your function to be async any more either.



          In addition, if you want your .catch() to still reject the promise, then you need to rethrow the error.





          If you wanted to keep the async/await, you would have to capture and return the value.



          async function Function2(yyyyReq) {
          console.log("Start - Function2");

          let val = await axios
          .post(some_other_url, yyyyReq)
          .then(res => {
          console.log('response '+res.status+' message '+res.data.message);
          console.log("End - Function2");
          return res;
          })
          .catch(error => {
          console.error("error " + error);
          console.log("End - Function2");
          throw error;
          });
          return val;
          }





          share|improve this answer


























          • Also added any as the return type for functions.

            – kpvsrkp
            Nov 25 '18 at 13:34
















          1














          Your async functions don't return any value, therefore the promise they return will have an undefined resolved value. The return statements you do have are inside .then() or .catch() callbacks. You don't have any return at the top level that actually sets the resolved value of the async function. You need to return the value you get from the await or get rid of the await and just return the promise.



          function Function2(yyyyReq) {
          console.log("Start - Function2");

          return axios
          .post(some_other_url, yyyyReq)
          .then(res => {
          console.log('response '+res.status+' message '+res.data.message);
          console.log("End - Function2");
          return res;
          })
          .catch(error => {
          console.error("error " + error);
          console.log("End - Function2");
          throw error;
          });
          }


          And, once you remove the await and just return the promise from the axios() call, you don't need your function to be async any more either.



          In addition, if you want your .catch() to still reject the promise, then you need to rethrow the error.





          If you wanted to keep the async/await, you would have to capture and return the value.



          async function Function2(yyyyReq) {
          console.log("Start - Function2");

          let val = await axios
          .post(some_other_url, yyyyReq)
          .then(res => {
          console.log('response '+res.status+' message '+res.data.message);
          console.log("End - Function2");
          return res;
          })
          .catch(error => {
          console.error("error " + error);
          console.log("End - Function2");
          throw error;
          });
          return val;
          }





          share|improve this answer


























          • Also added any as the return type for functions.

            – kpvsrkp
            Nov 25 '18 at 13:34














          1












          1








          1







          Your async functions don't return any value, therefore the promise they return will have an undefined resolved value. The return statements you do have are inside .then() or .catch() callbacks. You don't have any return at the top level that actually sets the resolved value of the async function. You need to return the value you get from the await or get rid of the await and just return the promise.



          function Function2(yyyyReq) {
          console.log("Start - Function2");

          return axios
          .post(some_other_url, yyyyReq)
          .then(res => {
          console.log('response '+res.status+' message '+res.data.message);
          console.log("End - Function2");
          return res;
          })
          .catch(error => {
          console.error("error " + error);
          console.log("End - Function2");
          throw error;
          });
          }


          And, once you remove the await and just return the promise from the axios() call, you don't need your function to be async any more either.



          In addition, if you want your .catch() to still reject the promise, then you need to rethrow the error.





          If you wanted to keep the async/await, you would have to capture and return the value.



          async function Function2(yyyyReq) {
          console.log("Start - Function2");

          let val = await axios
          .post(some_other_url, yyyyReq)
          .then(res => {
          console.log('response '+res.status+' message '+res.data.message);
          console.log("End - Function2");
          return res;
          })
          .catch(error => {
          console.error("error " + error);
          console.log("End - Function2");
          throw error;
          });
          return val;
          }





          share|improve this answer















          Your async functions don't return any value, therefore the promise they return will have an undefined resolved value. The return statements you do have are inside .then() or .catch() callbacks. You don't have any return at the top level that actually sets the resolved value of the async function. You need to return the value you get from the await or get rid of the await and just return the promise.



          function Function2(yyyyReq) {
          console.log("Start - Function2");

          return axios
          .post(some_other_url, yyyyReq)
          .then(res => {
          console.log('response '+res.status+' message '+res.data.message);
          console.log("End - Function2");
          return res;
          })
          .catch(error => {
          console.error("error " + error);
          console.log("End - Function2");
          throw error;
          });
          }


          And, once you remove the await and just return the promise from the axios() call, you don't need your function to be async any more either.



          In addition, if you want your .catch() to still reject the promise, then you need to rethrow the error.





          If you wanted to keep the async/await, you would have to capture and return the value.



          async function Function2(yyyyReq) {
          console.log("Start - Function2");

          let val = await axios
          .post(some_other_url, yyyyReq)
          .then(res => {
          console.log('response '+res.status+' message '+res.data.message);
          console.log("End - Function2");
          return res;
          })
          .catch(error => {
          console.error("error " + error);
          console.log("End - Function2");
          throw error;
          });
          return val;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 20:22

























          answered Nov 24 '18 at 20:05









          jfriend00jfriend00

          435k55567615




          435k55567615













          • Also added any as the return type for functions.

            – kpvsrkp
            Nov 25 '18 at 13:34



















          • Also added any as the return type for functions.

            – kpvsrkp
            Nov 25 '18 at 13:34

















          Also added any as the return type for functions.

          – kpvsrkp
          Nov 25 '18 at 13:34





          Also added any as the return type for functions.

          – kpvsrkp
          Nov 25 '18 at 13:34




















          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%2f53461806%2funable-to-retrieve-response-in-promise-all%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python