首页 > 解决方案 > reactjs:解析错误:await是保留字

问题描述

./src/App.js

第 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();

我怎样才能摆脱这个错误?

标签: javascriptasync-await

解决方案


由于您是这种语言的新手,我建议您不要使用这种方式。那就是所谓的arrow function

async () => { /*...*/ };

// same to

async function () { /*...*/ };

并将其与参数一起使用:

async (param_1, param_2) => { /*...*/ };

// same to

async function (param_1, param_2) { /*...*/ };

在您的情况下,问题可能来自

// remove "=" character after "async" keyword here
async=()=> { /*...*/ }

希望这可以帮助!


推荐阅读