Home>

I'm taking the sample code from the official @react-native-async-storage/async-storage page and pasting in my test data:

const setStringValue= async (value: string)=> {
  try {
    awaitAsyncStorage.setItem('key', value)
  } catch(e) {
    //save error
  }
  console.log('Done.')
}
const getMyStringValue= async()=> {
  try {
    return awaitAsyncStorage.getItem('key')
  } catch(e) {
    //read error
  }
  console.log('Done.')
}
setStringValue('qwerty');
console.log(getMyStringValue());

In the console I get this:

{"_40": 0, "_55": null, "_65": 0, "_72": null}

What is the reason?

  • Answer # 1

    You are trying to log the result of an asynchronous function. For this to work, you must first wait for the result of this function.
    For example like this:

    const fn= async()=> {
      const result= await getMyStringValue();
      console log(result);
    }
    fn();
    
  • Answer # 2

    You are trying to log the result of an asynchronous function. For this to work, you must first wait for the result of this function.
    For example like this:

    const fn= async()=> {
      const result= await getMyStringValue();
      console log(result);
    }
    fn();