为什么await不能在firefox上使用async

J D*_*Doe 3 javascript fetch async-await

基本上我有这个功能

async function get(url){
  const response = await fetch(url);
  const resData = await response.text();
  return resData;
}
Run Code Online (Sandbox Code Playgroud)

然后我接到这个电话

let data = await get(am_url);
Run Code Online (Sandbox Code Playgroud)

该代码在 google chrome 上完美运行,但在 firefox 上,我在调用线路上收到此错误:

语法错误:await 仅在异步函数和异步生成器中有效

这里有什么问题,对于我的一生,我似乎无法在 Firefox 上使用它,并且不明白为什么

例如,如果我在 firefox 和 google chrome 上打开 google.com,然后我转到控制台,并粘贴此代码,在 chrome 上,它将运行,但在 firefox 上,它将抛出我提到的错误

async function get(url){
  const response = await fetch(url);
  const resData = await response.text();
  return resData;
}

let data = await get("http://google.com");
console.log(data)
Run Code Online (Sandbox Code Playgroud)

Sum*_*mer 5

在 main 中,要么将下面的代码放入自执行异步函数中,要么使用 .then。

let data = await get(am_url);
Run Code Online (Sandbox Code Playgroud)

应该改为

(async()=>{ let data = await get(am_url) })()
Run Code Online (Sandbox Code Playgroud)

或者

get(am_url).then( data => ....)
Run Code Online (Sandbox Code Playgroud)

  • 这并不能解释为什么一个浏览器接受它而另一个浏览器不接受 (3认同)