admin管理员组

文章数量:1386529

basically i have this function

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

then later i have this call

let data = await get(am_url);

the code works perfectly on google chrome, but on firefox, i get this error on the call line :

SyntaxError: await is only valid in async functions and async generators

what's the problem here, for the life of me, i can't seem to make this work on firefox and can't figure out why

for example if i open google on firefox and google chrome, then i go to the console, and pase this code, on chrome, it will run, but on firefox, it will throw the error i mentionned

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

let data = await get("");
console.log(data)

basically i have this function

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

then later i have this call

let data = await get(am_url);

the code works perfectly on google chrome, but on firefox, i get this error on the call line :

SyntaxError: await is only valid in async functions and async generators

what's the problem here, for the life of me, i can't seem to make this work on firefox and can't figure out why

for example if i open google. on firefox and google chrome, then i go to the console, and pase this code, on chrome, it will run, but on firefox, it will throw the error i mentionned

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

let data = await get("http://google.");
console.log(data)
Share Improve this question edited Dec 27, 2018 at 5:05 J Doe asked Dec 27, 2018 at 4:48 J DoeJ Doe 411 silver badge3 bronze badges 8
  • 4 Sounds like data might not be in an async function? – CertainPerformance Commented Dec 27, 2018 at 4:50
  • data isn't a function, it's supposed to hold the text content of the response, and i tried both executing it from the console as well as from within a script file in the html page, same problem, works on chrome but not on firefox – J Doe Commented Dec 27, 2018 at 4:55
  • Why are you doing an await response.text() btw? – Aakash Verma Commented Dec 27, 2018 at 4:55
  • 3 @AakashVerma because Response.text() returns a Promise? – Kaiido Commented Dec 27, 2018 at 4:56
  • I didn't say data is a function (obviously it's not), I said that it wasn't in an async function - can you post the full code so we have a minimal reproducible example to figure out? – CertainPerformance Commented Dec 27, 2018 at 4:56
 |  Show 3 more ments

3 Answers 3

Reset to default 5

In main either put your below code in self executing async function or use .then.

let data = await get(am_url);

should be changed to

(async()=>{ let data = await get(am_url) })()

or

get(am_url).then( data => ....)

As the error suggests, await only works inside async functions. Normally you can't use await like that, but you have to create an async function first or use .then() instead of await.

However there are 2 things to be aware of:

  1. The console supports await outside an async function just to simplify your life. Chrome did it first, then Firefox more recently. Your example now works in both browsers.
  2. In a future version of ECMAScript, you will be able to use await outside async functions, it's called "top-level await", so that code will soon work everywhere (in a type="module" context)

In 2018 when this question was asked, most JavaScript consoles did not await at the top level.

At that time, Google Chrome Developer Tools console was the exception. They added the feature in late 2017 in Chrome 62

This is why, in the Firefox version you used when you asked this question, you have to resolve the promise, for example with then/catch.

If you update to a current Firefox, such as version 72 (early 2020), the code in your question will work. As fregante pointed out in a ment, in 2019, Firefox upgraded their console to support top level await.

本文标签: javascriptWhy isn39t await working with async on firefoxStack Overflow