3 回答
TA贡献2003条经验 获得超2个赞
我在下面的代码中放置了两个标记。我删除了这个_.forEach功能
mark1:使用普通的for循环来完成
mark2:这里使用await
//First API call to get [arr]
const results = await getlist();
// ########## mark1 ########## : Use normal for-loop to do it
for (const result of results) {
//Seconday request for each item in [arr]
const record = await item(result.id).fetch();
//Combined doc from original result and secondary call for record
let doc = new DocModel({
item1: result.id,
item2: record.something,
});
// ########## mark2 ########## : use await here
//Save doc
const saveDoc = await doc.save();
}
//Call for all docs
const allItems = await DocModel.find();
//Render all docs
res.render(`aView`, {
recordings: allItems,
});
TA贡献1829条经验 获得超6个赞
Promise.all
await Promise.all(_.map(results,async result => { ... existing code });
添加回答
举报