0

To replicate the below, run this code on React Native 0.59.8

I have a function that looks like the below:

import AsyncStorage from 'react-native';

const saveToStorage = async (value) => {
    await AsyncStorage.setItem('@store.key', value);
    let value = await AsyncStorage.getItem('@store.key');
    // Exception is thrown on ios because value is seen as null
}

But if I changed let to var the request is processed successfully.

const saveToStorage = async (value) => {
    await AsyncStorage.setItem('@store.key', value);
    var value = await AsyncStorage.getItem('@store.key');
    // No exception is thrown on ios
}

Does anyone have an idea what is going on?

F.O.O
  • 4,730
  • 4
  • 24
  • 34

1 Answers1

0

I would comment this if I could but I think there's a couple things that you could change. Don't name that variable value because you are passing a parameter called value into your async function, so value will already be defined. Also I think you are missing an await in your AsyncStorage.getItem('@store.key')

Sumant Guha
  • 101
  • 2
  • 7