Arrow功能与async和await in react native

时间:2017-05-01 11:04:11

标签: javascript react-native async-await react-native-android

我正在AsyncStorage react-native中保存数据。我想将其保存为异步,因此请使用asyncawait关键字。

  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);
    }
  };

我在保存程序时遇到错误

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 {

1 个答案:

答案 0 :(得分:15)

异步命名箭头函数应声明为

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);
    }
  };