Arrow functions with async and await in react native

25,352

Async named arrow function should be declared like

const onPositiveClickListener = async () => {
    // user has completed product tour_end
    try {
      await AsyncStorage.setItem("@ProductTour:key", "true");
      const { navigate } = this.props.navigation;
      navigate("DashboardScreen");
    } catch (error) {
      console.log(error);
    }
  };
Share:
25,352
N Sharma
Author by

N Sharma

I have done masters in Advanced Software Engineering. I have worked on various technologies like Java, Android, Design patterns. My research area during my masters is revolving around the Recommendation algorithms that E-commerce websites are using in order to recommend the products to their customers on the basis of their preferences.

Updated on July 17, 2020

Comments

  • N Sharma
    N Sharma almost 4 years

    I am trying to save data in AsyncStorage in react-native. I want to save it asynchronous so using async and await keyword.

      async onPositiveClickListener = () => {
        // user has completed product tour_end
        try {
          await AsyncStorage.setItem("@ProductTour:key", "true");
          const { navigate } = this.props.navigation;
          navigate("DashboardScreen");
        } catch (error) {
          console.log(error);
        }
      };
    

    I am getting an error while saving program

    SyntaxError: Unexpected token, expected ( (40:32)
      38 |   };
      39 | 
    > 40 |   async onPositiveClickListener = () => {
         |                                 ^
      41 |     // save user has completed product tour_end
      42 |     try {
      43 |       await AsyncStorage.setItem("@ProductTour:key", "true");
    Hide Stack Trace
    SyntaxError: Unexpected token, expected ( (40:32)
      38 |   };
      39 | 
    > 40 |   async onPositiveClickListener = () => {
         |                                 ^
      41 |     // save user has completed product tour_end
      42 |     try {