I got the above error while trying to follow the async example in the documentation. Luckily, a little searching and GitHub led me to GitHub Issue #5869. The fix suggested by the original poster was to fix the original syntax by declaring the function as a function like this:
async function getUsersFromApi()
But further down, there is a PR to fix the documentation by making sure the function is wrapped in a createClass method like this:
var MyComponent = React.createClass({
...
async getUsersFromApi() {
try {
let response = await fetch('https://mywebsite.com/endpoint/');
let responseJson = await response.json();
return responseJson.users;
} catch(error) {
throw error;
}
}
...
})
Both solutions make sense to me and both get the job done. I ended up going with the second solution. I hope this helps save someone a little time (but I’m guessing they’ll update the documentation soon enough).