1

I'm hazy on async, and I'm not sure why I'm getting a syntax error in my react component while trying to write a simple async method.

class Locations extends React.Component {
  constructor(props) {
    super(props);

    this.searchProducts = this.searchProducts.bind(this);

  }

    async searchProducts(product_name, storeId) {
    let response =  await axios.get(
      `https://api.goodzer.com/products/v0.1/search_in_store/?storeId=${storeId}&query=${product_name}&apiKey=125cbddf3880cb1ba652a7c269ba1eb0`
    );
    console.log(response);
    return response.data.products;

}

  componentWillMount() {
    let products = [];
    this.props.searchLocations(this.props.navigation.state.params.product_name, this.props.navigation.state.params.searchRadius)
      .then(
          products = (this.props.locations.map(
          location => ({[location.id]: this.searchProducts(
            this.props.navigation.state.params.product_name, location.storeid
          )}))
        )
      );

      console.log(products);
  }

  render() {
    const displayLocations = this.props.locations.map(
      location => (<Text key={location.id}>{location.name}</Text>)
    );
    return (
      <View>
        <Text>HELLO</Text>
        <View>{displayLocations}</View>
      </View>
    );
  }
}

I'm getting a syntax error: 'Unexpected token, expected ('.

I don't understand how this is any different than the examples given here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/async_function

5
  • Are you using babel? Can you share your babel config? Commented Oct 15, 2017 at 5:25
  • At which line does error occur? Note, this.searchProducts() returns a Promise object Commented Oct 15, 2017 at 5:26
  • That's the babelrc? { "presets": ["babel-preset-expo"], "env": { "development": { "plugins": ["transform-react-jsx-source"] } } } Commented Oct 15, 2017 at 5:32
  • The error is on line 32 Commented Oct 15, 2017 at 5:33
  • Also, I thought using 'await' means that 'response' will not be a promise by the time the function returns? Commented Oct 15, 2017 at 5:35

2 Answers 2

2

The syntax error is at

  .then(
      products = /* missing `>` at arrow function */ (this.props.locations.map(
      location => ({[location.id]: this.searchProducts(
        this.props.navigation.state.params.product_name, location.storeid
      )}))
    )
  )

.then() expects a function as argument, this.searchProducts() returns a Promise object, async also need to be set within .map() callback function

async componentWillMount() {
  let products = await this.props
  .searchLocations(
    this.props.navigation.state.params.product_name, 
    this.props.navigation.state.params.searchRadius
  )
  .then(
    () => 
        Promise.all(this.props.locations.map(async(location) =>
          ({[location.id]: await this.searchProducts(
            this.props.navigation.state.params.product_name, 
            location.storeid
          )}))
        )
  );
  console.log(products);
}
Sign up to request clarification or add additional context in comments.

10 Comments

I replace my .then() with your revision but I still get Syntax error: await is a reserved word
@killer_manatee See updated post. async needs to be set at the function where await is used, that is, at .map() callback
Ok, the syntax error is gone. Do you know how I would actually get the result of the Promise.all and store it in the array? What I want is an array of objects with location.id as the key and the result of searchProducts as the value. Thanks
products is the array. Have not tried reactjs, not sure if products should be returned from ComponentWillMount?
I'm confused about how to access the data that the promise eventually returns. Shouldn't the promise eventually return an array of objects? I've done some reading about promises but I don't understand how to 'get' them when they resolve
|
0

Your componentDidMount function is syntactically invalid. You have not supplied a proper function to the then call. Also, your console.log should be inside the then call.

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.