我正在AsyncStorage
react-native
中保存数据。我想将其保存为异步,因此请使用async
和await
关键字。
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 {
答案 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);
}
};