这是我在nodejs中的简单函数
const myFunction = async() => {
const exercises = await Exercise.find({ workoutId })
return exercises
}
const value = await myFunction()
但是当我在异步函数之外执行 await
时它会抛出错误
await is a reserved word
现在如何在异步函数之外等待值?我需要使用回调还是 .then
?那async and await
有什么用呢?
最佳答案
您不能在异步函数外使用 await
。
“绕过”此限制的一个技巧是使用异步 IFEE:
const myFunction = async() => {
const exercises = await Exercise.find({ workoutId })
return exercises
};
(async () => {
const value = await myFunction()
})()
关于javascript - 是否可以在异步函数之外使用 await,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52580821/