简体   繁体   English

全局调用异步 function 时出错:“等待仅在异步函数和模块的顶层主体中有效”?

[英]Error in global call to async function: "await is only valid in async functions and the top level bodies of modules"?

Before I begin, I acknowledge that there are several questions on SO which may sound similar to mine going by the title, however, all of them that I read are more complicated than my code and the explanation does not seem to pertain to my situation.在开始之前,我承认有几个关于 SO 的问题可能听起来我的标题相似,但是,我阅读的所有问题都比我的代码复杂,并且解释似乎与我的情况无关。

Can someone please help me understand what is going on in my code (snippet below) that is resulting in this error:有人可以帮助我了解导致此错误的代码(下面的代码段)中发生了什么:

Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules. Uncaught SyntaxError: await 仅在异步函数和模块的顶层主体中有效。

As far as I can see, the await which is giving rise to the error is in a "top level" body.据我所见,导致错误await位于“顶级”正文中。 Or is something else meant by top level body?还是顶级机构的其他含义? Thanks a lot!非常感谢!

EDIT for differentiating from the other suggested (similar) question here : My question does not deal with httpGet, some other context is different, and most importantly I have received an answer that solves the problem for me, unlike the suggestion given in the (solitary) answer to the other question.编辑在这里与其他建议的(类似)问题区分开我的问题不涉及httpGet,其他一些上下文不同,最重要的是我收到了一个为我解决问题的答案,这与(单独的)中给出的建议不同) 回答另一个问题。 Hence, while I have been able to find a solution here, I believe it will be valuable for the general audience for my question to stay.因此,虽然我能够在这里找到解决方案,但我相信对于普通观众来说,留下我的问题将是有价值的。

 var data; await getData(); document.body.write(data); async function getData() { const res = await fetch("https://jsonplaceholder.typicode.com/posts", { method: 'GET', headers: { 'Accept': 'application/json, text/plain, */*', 'Content-type': 'application/json' } }); data = await res.json(); }

The top-level await means that you are trying to use async/await syntax outside async function.顶级await意味着您正在尝试在async function 之外使用async/await语法。 The workaround is to create some function eg main and put the code in it.解决方法是创建一些 function 例如main并将代码放入其中。

async function main() {
  var data;
  await getData();
  document.body.write(data);
}

main();

One day top-level async/await will be supported and there is a proposal for it.有一天将支持顶级异步/等待,并且有一个提案。 Meanwhile you can use this babel plugin to use it https://babeljs.io/docs/en/babel-plugin-syntax-top-level-await without wrapper function like main .同时你可以使用这个 babel 插件来使用它https://babeljs.io/docs/en/babel-plugin-syntax-top-level-await没有包装器 function 像main

Yes, it is global code - in my script.js file.是的,它是全局代码 - 在我的 script.js 文件中。 And I thought what could be more "top-level" than that?我想还有什么比这更“顶级”的呢?

As pointed out in a comment, the problem isn't "top level" it is "in a module".正如评论中指出的那样,问题不是“顶级”,而是“在模块中”。

Web browsers will load scripts as modules only if the type attribute says to do so: Web 浏览器仅在type属性要求时才会将脚本作为模块加载:

<script type="module" src="script.js"></script>

This enables support for import (CORS permitting), makes the script load asynchronously, and stops the top level scope being global.这支持import (CORS 允许),使脚本异步加载,并停止顶级 scope 是全局的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 async await 在 chrome 扩展中出现此错误“await 仅在异步函数和模块的顶层主体中有效” - Getting this error "await is only valid in async functions and the top level bodies of modules" in chrome extension with async await SyntaxError: await 仅在异步函数和模块的顶层主体中有效 - SyntaxError: await is only valid in async functions and the top level bodies of modules await 仅在异步函数和循环中模块的顶层主体中有效 - await is only valid in async functions and the top level bodies of modules in loop await 仅在异步函数和模块的顶级主体中有效 - await is only valid in async functions and the top level bodies of modules javascript await function in script 返回 Uncaught SyntaxError: await is only valid in async functions and the top level body of modules - javascript await function in script returns Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules await 仅在异步函数和模块的顶层主体中有效 javascript 表示错误 - await is only valid in async functions and the top level bodies of modules javascript express error ASYNC / AWAIT SyntaxError: await 仅在异步函数和模块的顶层主体中有效 - ASYNC / AWAIT SyntaxError: await is only valid in async functions and the top level bodies of modules 打开 AI 示例代码不起作用“等待仅在异步函数和模块的顶级主体中有效” - Open AI example code not working "Await is only valid in async functions and the top level bodies of modules" SyntaxError: await 仅在异步函数和模块的顶层主体中有效。 不和谐,js - SyntaxError: await is only valid in async functions and the top level bodies of modules. Discord,js async/await 模块语法错误的顶级主体 - Top level bodies of modules syntax error with async/await
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM