Waiting for AsyncStorage.getItem()

前端 未结 2 1777
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 08:36

I am using AsyncStorage in my React Native application to store information about the user. The getItem() function is asynchronous and requires me

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 09:20

    You can try either add a then after your getItem.

    AsyncStorage.getItem("phoneNumber").then((value) => {
        this.setState({"phoneNumber": value});
    })
    .then(res => {
        //do something else
    });
    

    Or use await to wait the async operation to finish

    var value = await AsyncStorage.getItem(STORAGE_KEY);
    //use value to do something else.
    

提交回复
热议问题