Reputation: 239
I am using AsyncStorage to store and retrive some information (like name, email) in application. when i call setItem it returns the promise "_45":0,"_65":0,"_55":null:"_75":null and getItem also return the same. How do i set/get the value in AsyncStorage? I am using the following code :
export async function setItem(key, value) {
try {
var data = await AsyncStorage.setItem(key, value);
} catch (error) {
}
}
export async function getItem(key) {
try {
var item = await AsyncStorage.getItem(key);
if(item !== null) {
return item;
}
} catch (error) {
}
}
Thanks in advance.
Upvotes: 1
Views: 1038
Reputation: 1348
The return value of an async
function is wrapped in a promise. Inside your getItem
, you can access item
as a standard object, but callers of getItem
need to either (1) be an async function or (2) treat the return value as a Promise.
Upvotes: 0
Reputation: 3627
Your function return promises. async/await
is just a syntactic sugar. If you want to get item from AsyncStorage
using your helper you need to use like that:
getItem('someKey')
.then(val => console.log(val)
.catch(err => console.log(err))
or
function async getItemAndDoSomething() {
const item = await getItem('someKey')
// do something with your item here
}
getItemAndDoSomething()
Upvotes: 2