Dar*_*ght 1 javascript asynchronous node.js async-await ecmascript-2017
这是我在 nodejs 中的简单功能
const myFunction = async() => {
    const exercises = await Exercise.find({ workoutId })
    return exercises
}
const value = await myFunction()
Run Code Online (Sandbox Code Playgroud)
但是当我await在异步函数之外执行它时会引发错误
 await is a reserved word
Run Code Online (Sandbox Code Playgroud)
现在我如何等待异步函数之外的值?我需要使用回调.then吗?那有什么用async and await呢?
小智 5
您不能await在异步函数之外使用。“绕过”此限制的一个技巧是使用异步 IFEE:
const myFunction = async() => {
    const exercises = await Exercise.find({ workoutId })
    return exercises
};
(async () => {
    const value = await myFunction()
})()
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           9300 次  |  
        
|   最近记录:  |