Javascript 如何在for-each循环中使用异步等待?

Javascript 如何在for-each循环中使用异步等待?,javascript,node.js,async-await,Javascript,Node.js,Async Await,我正在从事一个项目,其中有一个数组,我需要通过它从我的MongoDB数据库中查询模型 在这个查询完成之后,我需要用查询响应增加另一个数组,但是在我使用wait查询之后,查询下的所有内容似乎都被“忽略” 我试图回报一个承诺,甚至使用.then(),但没有任何效果 const schedules = { all: [], unique: [], final: []}; ... schedules.unique.forEach(async (schedule) => { const final

我正在从事一个项目,其中有一个数组,我需要通过它从我的MongoDB数据库中查询模型

在这个查询完成之后,我需要用查询响应增加另一个数组,但是在我使用wait查询之后,查询下的所有内容似乎都被“忽略”

我试图回报一个承诺,甚至使用.then(),但没有任何效果

const schedules = { all: [], unique: [], final: []};
...
schedules.unique.forEach(async (schedule) => {
const final = await ScheduleRef.findById(schedule);
  schedules.final.push(final);
});

假设
findById
返回承诺是正确的,您应该能够通过使用
map
迭代
时间表.unique
,然后将
wait
ed
promise.all
的值分配给
时间表.final
来收集所有承诺

const schedules={all:[],unique:[],final:[]};
(异步()=>{
const finds=schedules.unique.map(schedule=>{
返回ScheduleRef.findById(附表);
});
schedules.final=等待承诺。全部(发现);
})();你看过这个吗: