Waiting for AsyncStorage.getItem ()

I use AsyncStorage in my React Native app to store user information. The getItem() function is asynchronous and requires me to call back when I want to load data from the storage system.

 AsyncStorage.getItem("phoneNumber").then((value) => { this.setState({"phoneNumber": value}); }).done(); 

Since it takes a little time to retrieve the value from the repository, I would like to wait for the operation to complete before continuing.

Is it possible to load data in a way that is not asynchronous? If not, is there an easy way to wait for the getItem() call to complete getItem() continuing?

+6
source share
2 answers

You can try either add a after your getItem.

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

Or try to wait for the async operation to complete

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

try this option and you won’t get "
Syntax error: Pending is a reserved word "

  async getData(){ return await AsyncStorage.getItem("@App:KEY"); } 
+3
source

All Articles