1
votes

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.

2

2 Answers

2
votes

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()
0
votes

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.