0

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)

1

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.