I am trying to run the following code block from the official react-native docs :
async function getMoviesFromApi() {
try {
let response = await fetch('https://facebook.github.io/react-native/movies.json');
let responseJson = await response.json();
return responseJson.movies;
} catch(error) {
console.error(error);
}
}
When I try running this I get the error:
"Unexpected token, expected ( (31:17)"
If I remove the function
keyword, it works fine.
What difference does it make if I use the function keyword or not? Is it because I am using it in a class? Where in the documentation does it indicate this? Is it in the react-native documentation or the JavaScript documentation? I cannot seem to find it either place, though I could be searching for the wrong thing.
I am using react-native: 0.38.0 (with react-native-cli: 1.2.0)
Is it because I am using it in a class?
Probably. The syntax for declaring methods in a class is
class Foo {
method() {}
}
not
class Foo {
function method() {}
}
Hence an async method is declared as
class Foo {
async method() {}
}
Where in the documentation does it indicate this? Is it in the react-native documentation or the JavaScript documentation?
It's JavaScript. React is a framework/library, not a language. All the syntax you are using is JavaScript (except JSX of course, but that's not specific to React either).
See the MDN documentation about classe s.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.