<script type="module">
function fn() {
return Promise.resolve(1)
}
let res = await fn();
console.log(res);
</script>
正常
要点:
type="module"
启用了 ES6 模块模式- 模块顶层支持
await
(无需包裹在 async 函数中)
<script>
function fn() {
return Promise.resolve(1)
}
let res = await fn();
console.log(res);
</script>
JavaScript 规范规定:await
只能在 async
函数内部或模块顶层**使用。