Reputation: 23
I have an async function that calls AsyncStorage.getItem but always returns a promise.
I tried to use .then clause but the result is similar
I tried to use AsyncStorage.getItem out of the function but I get the error "await is a reserved word"
getDataStorage = async () => {
console.log("getDataStorage");
var data = '';
try {
data = await AsyncStorage.getItem('dataStorage');
console.log("getting data " + data);
return data;
} catch (error) {
console.log("----" + error.message);
}
};
componentDidMount(){
console.log("componentDidMount");
var data = this.getDataStorage();
console.log(data);
}
The result is first displays the promise then prints the value that I get with getItem().
I want to get the value, I suppose with await the function waits for the result of getItem, is it correct?
Upvotes: 0
Views: 218
Reputation: 478
Yes, await functions wait for the result. But, in your case its only waiting till returning promise, so you have to change your code as:
componentDidMount = async () => {
console.log("componentDidMount");
data = await this.getDataStorage();
console.log(data);
}
Upvotes: 2