我有一个异步函数(控制器),里面有多个等待对象。例如,我可以有这样的东西:
import re
....
print(special_code)
jsonStr = re.search(r'(\{.*?\});', special_code, re.S).group(1)
combinationsFromController = json.loads(jsonStr)
for key in combinationsFromController:
print(key)
# 224114
# 224140
# 224160
所以我的疑问是:
如果我需要在数据库中编写3个独立文档,如何确定所有3个文档都已执行?我不想让一个失败,或者如果失败,我想确保回退另一个。像这样
exports.check_in = async (req, res, next) => {
try {
const place= await Place.findOne({_id: req.params.placeId});
// MY SECOND DEPENDS ON THE RESULT OF THE FIRST ONE
const area= await Area.findOne(_id: place.areaId);
const client = await Client.findOne({_id: req.body.clientId});
// BUSINESS LOGIC HERE
} catch (error) {
next(error);
}
}