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 anasync
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 anasync
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
3 Answers
Reset to default 5In 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:
- The console supports
await
outside anasync
function just to simplify your life. Chrome did it first, then Firefox more recently. Your example now works in both browsers. - In a future version of ECMAScript, you will be able to use
await
outsideasync
functions, it's called "top-levelawait
", so that code will soon work everywhere (in atype="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
版权声明:本文标题:javascript - Why isn't await working with async on firefox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744510224a2609837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论