猫鼬等待不工作

背景代码
async function main() {
  console.log("Start")
  await Author.create({name: 'Some Guy'}, function (err, awesome_instance) {
    console.log("This should be next, but its not")
  });

  console.log("This should be last, but it's not")
}

main()

最后一条日志语句是在第二条之前记录日志。如果我正在使用,为什么会出现这种情况await

这将是烧瓶

Mongoose 方法在指定回调时切换到回调模式,否则切换到承诺模式。

它应该是:

async function main() {
  try {
    await Author.create({name: 'Some Guy'});
  } catch (err) {
    console.error(err);
  }
  console.log("This should be next, but its not");
  console.log("This should be last, but it's not");
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章