[英]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 OUTPUT
promiseValue [ [ Promise { <pending> } ], [ Promise { <pending> }, Promise { <pending> } ], [
Promise { <pending> },
Promise { <pending> },
Promise { <pending> } ] ]
I used Promise.all() to handle this nested async still get this result so How can I resolve this problem and why it shows this error我使用 Promise.all() 来处理这个嵌套异步仍然得到这个结果所以我该如何解决这个问题以及为什么它显示这个错误
You use Promise.all
only on the outer layer of the nesting, not on the inner array of promises.您只在嵌套的外层使用Promise.all
,而不是在 promise 的内部数组上使用。 You would also need to do你还需要做
return Promise.all(listofTerminalId.map(async (item) => {
// ^^^^^^^^^^^
return this.modelSettlement
.find({ terminalID: item })
.catch((err) => console.log(err));
}));
However, given the listofTerminalId.push(...terminalId)
, it looks like you actually don't want any nesting.但是,鉴于listofTerminalId.push(...terminalId)
,看起来您实际上不想要任何嵌套。 You may just want to你可能只想
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);
}
You need to pass an array of promises to Promise.all
method that means you should neither await
the promises nor .then
them before passing them to Promise.all
.您需要将一系列承诺传递给Promise.all
方法,这意味着在将它们传递给.then
之前,您既不应该await
承诺也不应该等待Promise.all
。
So it might look like this:所以它可能看起来像这样:
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.