繁体   English   中英

如何从嵌套异步 function 返回数据

[英]How to return data from nested async function

async getSettlementByUser(email): Promise<any> {
    let listofTerminalId = [];
    // Get current user
    let user = await this.userModel.findOne({ email: email });
    // get list of sn
    let tpeIndexes = user.bindedSn.map((item) => item);
    //list des tpes binded to current user
    let tpe = tpeIndexes.map(async (index) => {
      let list = await this.tpeModel.find({ sn: index });
      // console.log('liste des tpes', list);
      let terminalId = list.map((item) => item.terminalId);
      // console.log('terminalId', ...terminalId);
      listofTerminalId.push(...terminalId);

      console.log('listofTerminalId', listofTerminalId);
      return await listofTerminalId.map(async (item) => {
        return await this.modelSettlement
          .find({ terminalID: item })
          .then((res) => res)
          .catch((err) => console.log(err));
      });
      // console.log('settlement', settlement);
      // return settlement;
    });

    let promiseValue = await Promise.all(tpe);
    console.log('promiseValue', promiseValue);
    // // return promiseValue;
    // return tpe;
  }

OUTPUT

promiseValue [   [ Promise { <pending> } ],   [ Promise { <pending> }, Promise { <pending> } ],   [
    Promise { <pending> },
    Promise { <pending> },
    Promise { <pending> }   ] ]

我使用 Promise.all() 来处理这个嵌套异步仍然得到这个结果所以我该如何解决这个问题以及为什么它显示这个错误

您只在嵌套的外层使用Promise.all ,而不是在 promise 的内部数组上使用。 还需要做

return Promise.all(listofTerminalId.map(async (item) => {
//     ^^^^^^^^^^^
  return this.modelSettlement
    .find({ terminalID: item })
    .catch((err) => console.log(err));
}));

但是,鉴于listofTerminalId.push(...terminalId) ,看起来您实际上不想要任何嵌套。 你可能只想

async getSettlementByUser(email): Promise<any> {
  // Get current user
  const user = await this.userModel.findOne({ email: email });

  // list des tpes binded to current user
  const tpes = await Promise.all(user.bindedSn.map(async (index) => {
    const list = await this.tpeModel.find({ sn: index });
    // console.log('liste des tpes', list);
    return list.map((item) => item.terminalId);
  }));
  const listofTerminalId = tpes.flat();
  console.log('listofTerminalId', listofTerminalId);

  const settlements = await Promise.all(listofTerminalId.map((item) => {
    return this.modelSettlement.find({ terminalID: item })
  }));
  console.log('settlements', settlements);
}

您需要将一系列承诺传递给Promise.all方法,这意味着在将它们传递给.then之前,您既不应该await承诺也不应该等待Promise.all

所以它可能看起来像这样:

async getSettlementByUser(email): Promise<any> {
    let listofTerminalId = [];
    let user = await this.userModel.findOne({ email: email });
    let tpeIndexes = user.bindedSn.map((item) => item);

    let tpe = tpeIndexes.map(async (index) => {
        let list = await this.tpeModel.find({ sn: index });
        let terminalId = list.map((item) => item.terminalId);
        listofTerminalId.push(...terminalId);
  
        return listofTerminalId.map((item) => {
            return this.modelSettlement
              .find({ terminalID: item })
        });
    });

    let promiseValue = await Promise.all(tpe)
    console.log('promiseValue', promiseValue);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM