[英]reactjs :Parsing error: await is a reserved word
./src/App.js ./src/App.js
Line 15: Parsing error: await is a reserved word 第15行:解析错误:await是保留字
13 | getWeather = async=()=>{
14 |
15 | const api_call = await fetch('http://api.openweathermap.org/data/2.5/weather?q=Manchester,uk&appid=${API_KEY}');
| ^
16 |
17 | const data = await api_call.json();
How can I get rid of this error? 如何摆脱这个错误?
Since you're new to this language, I suggest you don't use this way. 由于您是这种语言的新手,因此建议您不要使用这种方式。 That's called arrow function
. 那就是arrow function
。
async () => { /*...*/ };
// same to
async function () { /*...*/ };
And use it with parameter(s): 并与参数一起使用:
async (param_1, param_2) => { /*...*/ };
// same to
async function (param_1, param_2) { /*...*/ };
In your case, the problem may come from 就您而言,问题可能来自
// remove "=" character after "async" keyword here
async=()=> { /*...*/ }
Hope this helps! 希望这可以帮助!
As others have mentioned, you have an unnecessary =
symbol. 正如其他人提到的那样,您有一个不必要的=
符号。 The async keyword doesn't need an =
symbol after it, you might think of it as a sort of 'label' for the function. async关键字后面不需要=
符号,您可能会将其视为函数的一种“标签”。 Because the function isn't correctly labelled as async
, the code doesn't like there being an await
keyword in the body of the function. 由于该函数未正确标记为async
,因此代码不喜欢在函数主体中使用await
关键字。
Here are some snippets to demonstrate the difference: 以下是一些片段,以说明两者之间的区别:
const getWeather = async=()=>{ const api_call = await fetch('http://api.openweathermap.org/data/2.5/weather?q=Manchester,uk&appid=${API_KEY}'); const data = await api_call.json(); }
The code above is trying to set both getWeather
and async
to be the function you define. 上面的代码试图将getWeather
和async
都设置为您定义的函数。 Here are some more examples to demonstrate: 以下是一些更多的示例来演示:
const test = aNonKeyword = () => { console.log('test') } const testTwo = anotherNonKeyword = 'A Test String' var var1 = var2 = var3 = 1 console.log(test) console.log(aNonKeyword) console.log(testTwo) console.log(var1) console.log(var2) console.log(var3)
...and here's the actual, working version: ...这是实际的工作版本:
const getWeather = async () => { const api_call = await fetch('http://api.openweathermap.org/data/2.5/weather?q=Manchester,uk&appid=${API_KEY}'); const data = await api_call.json(); }
Line 13 is trying to reassign an arrow function to the reserved variable "async". 第13行试图将箭头功能重新分配给保留变量“ async”。 Most likely a typo, one key to remember in J's is the right to left execution. 从右到左执行是错字,在J中要记住的一个关键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.