# SyntaxError: 'await' 出现在 async 函数之外

- **ID:** `python/asyncio-await-outside-async-function`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

await 关键字被用在普通的 def 函数或模块顶层。await 只能在 async def 函数体内使用（或在 IPython/Jupyter 的顶层 await 中）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   async def handler():
    data = await fetch()
    return data
   ```
2. **** (93% 成功率)
   ```
   async def main():
    await fetch()
if __name__ == '__main__':
    asyncio.run(main())
   ```
3. **** (90% 成功率)
   ```
   # IPython/Jupyter supports top-level await in cells
await fetch()
   ```

## 无效尝试

- **** — If the sync function is itself called from async code, this raises 'This event loop is already running'. (75% 失败率)
- **** — Coroutine objects have no result() method; raises AttributeError. (95% 失败率)
