Node.js 如何强制函数在继续之前等待承诺?

Node.js 如何强制函数在继续之前等待承诺?,node.js,async-await,Node.js,Async Await,我读了另一篇文章,但它们并没有解决我的特殊问题 在删除用户之前,我正在尝试将所有依赖项从一个用户移动到默认用户。但是,即使使用Promise.all().then(),我也会收到一个错误,说我仍然有外键约束阻止我删除该用户(但是如果我尝试在之后手动删除该用户,则效果很好,因为我确实将所有依赖项移到了默认用户) constchangefpromises=recipes.map(recipe=>{ Recipe.update(Recipe.id,{chef_id:1}) }); 这并不是创造一系列

我读了另一篇文章,但它们并没有解决我的特殊问题

在删除用户之前,我正在尝试将所有依赖项从一个用户移动到默认用户。但是,即使使用Promise.all().then(),我也会收到一个错误,说我仍然有外键约束阻止我删除该用户(但是如果我尝试在之后手动删除该用户,则效果很好,因为我确实将所有依赖项移到了默认用户)

constchangefpromises=recipes.map(recipe=>{
Recipe.update(Recipe.id,{chef_id:1})
});
这并不是创造一系列你所期待的承诺。您要么需要删除arrow函数的主体,要么在主体内部显式返回承诺

例如

constchangefpromises=recipes.map(recipe=>recipe.update(recipe.id,{chef_id:1}));

constchangefpromises=recipes.map(recipe=>{
返回Recipe.update(Recipe.id,{chef_id:1})
});
另外,混合使用
async
/
await
.then()
也有点奇怪,所以最好改变一下:

等待承诺。全部(更改承诺)。然后(()=>{
删除(id);
});


非常感谢你的帮助,效果非常好。我今年才开始学习js,我还有很多东西要学。我从来没有在同一个函数中使用
.then()
异步/await
,但由于使用
await
没有得到想要的结果,我想我可以使用
强制函数等待。我从来没有想到我在做出承诺的过程中会犯错误。你的回答真的很有帮助,我今天确实学到了一些我认为我已经知道的新东西。再次感谢你!
async delete (req, res){
try {
  const {id} = req.body;

  // Here I call all the recipes that this user owns
  const recipes = await Recipe.find({where: {chef_id: id}});

  // Create the Promises for every UPDATE request in the Database
  const changeChefPromises = recipes.map( recipe => {
    Recipe.update(recipe.id,{chef_id : 1})
  });

  /* HERE is the problem, I'm trying to update every single recipe
     before I delete the User, but my program is trying to delete
     the User before finishing the Update process for all dependencies */ 

  await Promise.all(changeChefPromises).then(() => {
    Chef.delete(id);
  });


  return res.redirect(`/admin/chefs`);

} catch (err) {
  console.error(err);
}

}
await Promise.all(changeChefPromises);
await Chef.delete(id);