Reputation: 100
I am using Asyncstorage to store user data like:
try {
await AsyncStorage.setItem(prod_id, '1').then(()=>{
alert('Added to Cart');
});
} catch (error) {
console.log(error);
}
but when I add it to onPress action it takes long time to save data and then my alert is called. Am I doing something wrong? Please I need help!
Upvotes: 0
Views: 2049
Reputation: 3426
I think you are mixing two ways of handling the promises in JS.
async function SetItem(){
try {
await AsyncStorage.setItem(prod_id, '1')
alert('Added to Cart');
} catch (error) {
console.log(error);
}
}
function SetItem(){
return AsyncStorage.setItem(prod_id, '1')
.then(()=>{
alert('Added to Cart');
}).catch((error)=> {
console.log(error)
})
}
calling this method
this.SetItem().then(()=>{
console.log("value is set");
})
Upvotes: 1