Hari Mohan
Hari Mohan

Reputation: 101

reactjs :Parsing error: await is a reserved word

./src/App.js

Line 15: Parsing error: await is a reserved word

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?

Upvotes: 1

Views: 3866

Answers (3)

OliverRadini
OliverRadini

Reputation: 6467

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. 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.

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. 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();
}

Upvotes: 0

rc_dz
rc_dz

Reputation: 461

Line 13 is trying to reassign an arrow function to the reserved variable "async". Most likely a typo, one key to remember in J's is the right to left execution.

Upvotes: 0

Tân
Tân

Reputation: 1

Since you're new to this language, I suggest you don't use this way. That's called 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!

Upvotes: 2

Related Questions