I am trying to pull json data after storing it in AsyncStorage on an android emulator. I can pull the data inside an async function, but I can't store it in a variable to be used elsewhere in my code
The following code will pull the data, but I cannot call variable value any where else in my code:
async function getValue(){
try {
const value = await AsyncStorage.getItem('redditInsights')
console.log(value)
return value
}
catch (error) {
console.log(error)
}
}
If I define a variable like so: const insights = getValue() outside the above function, it seems to try an define insights before the getValue() function is finished retrieving the data and so returns it as an empty object
Your help is greatly appreciated!!
return valueto the place wheregetValuewas called? Or, if you want, instead of a return, create a global variable and assing its value = tovalue- Calvin Nunesglobal.myGlobalVar = value, then I think you can use this variable anywhere since it is global... - Calvin Nunesconst insights = await getValue();- jvgaeta