While are you want to use await keyword without async directly in the scope with the await, Then you will face Unexpected reserved word ‘await’, this error.
In this article, we will discuss about this Unexpected reserved word ‘await’ error in ReactJs. And how to resolve the error all the possible solutions with examples.
How Unexpected reserved word ‘await’ Error Occurs?
Most of the time you get this error when the await keyword is used inside of a function that was not marked as async.
Unexpected reserved word 'await'
How To Fix Unexpected reserved word ‘await’ Error?
There are different ways to fix Unexpected reserved word ‘await’ this error. Let us take a look at every solution.
How To Fix Unexpected reserved word ‘await’ Error?
To Solve Unexpected reserved word ‘await’ Error If you not declare your function as a async you can’t able to use await. So, you have to set your function as a async and then you use await. Now, your error will be solved.
Unexpected reserved word ‘await’
To Solve Unexpected reserved word ‘await’ Error If you not declare your function as a async you can’t able to use await. So, you have to set your function as a async and then you use await. Now, your error will be solved.
Solution 1: declare Async
If you not declare your function as a async you can’t able to use await keyword. So, you have to set your function as a async and then you use await.
async function getString() { // Declare async Here
const myData = await axios.get('my_api_call); // Now you can use Await
return myData;
}
Don’t forget that the directly enclosing function has to be marked as async for you able to use the await keyword.
async function loopThrough() {
['a', 'b', 'c'].forEach(str => {
// unexpected reserved word 'await'
await Promise.resolve(str);
});
}
Now, Your error will be solved.
Conclusion
In this article, we have discussed what causes the error and we have discussed ways to fix the error.
we hope this article has been informative. Thank you for reading. Kindly comment and let us know if you found it helpful.