【问题标题】:Unable to retrieve response in promise.all无法在 promise.all 中检索响应
【发布时间】:2019-04-26 23:00:18
【问题描述】:

我正在进行如下所示的并行调用。

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);

下面是函数1。

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;
    });
}

下面是函数2。

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;
    });
}

我能够并行调用函数 1 和 2,并且能够在函数中获得响应。 但是,当 Promise 被解析并按照输入的顺序收集响应时,响应不是从 Promise 中收集的。 日志说 null 被传递到 Promise.all 响应数组中。

下面是执行日志。

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

我在这里缺少什么。

【问题讨论】:

    标签: node.js typescript firebase google-cloud-functions


    【解决方案1】:

    您的async 函数不返回任何值,因此它们返回的承诺将具有undefined 解析值。您拥有的 return 语句在 .then().catch() 回调中。您在顶层没有任何实际设置 async 函数的解析值的返回值。您需要返回从 await 获得的值或摆脱 await 并返回承诺。

    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;
        });
    }
    

    而且,一旦您删除 await 并从 axios() 调用中返回承诺,您也不再需要您的函数为 async

    此外,如果您希望您的.catch() 仍然拒绝该承诺,那么您需要重新抛出错误。


    如果您想保留async/await,则必须捕获并返回该值。

    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;
    }
    

    【讨论】:

    • 还添加了 any 作为函数的返回类型。
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多