config.js
AsyncStorage.getItem("storedValue").then(value=>{
//return data based on the value from asyncStorage storage
if(value==="value1"){
return {
text:"text1",
number:"number1"
}
}
else if(value==="value2"){
return {
text:"text2",
number:"number2"
}
}
})
index.js
import config from "./config"
//I have to use data from config file here
console.log(config.text) //->getting error
//The problem is that, since fetching from AsyncStorage is asynchronous, data from config is not yet available here
//I have tried SyncStorage npm package for react native. It loads all the asyncStored values into memory upon calling
await SyncStorage.init() //-> called on app initialisation
//And can be used later in place of AsyncStorage.getItem(""), which returns the value directly instead of promise
const value=SyncStorage.get("storedValue")
But the problem is, I am accessing the config in index.js(which is the starting file for a react-native project). And I have to initialise SyncStorage here, which is again asynchronous, which means the config file will get loaded before init() happens.
What I am expecting is:
ORORChanges that are not possible:
Thank you.
Please explain a little further...
Summarising what I understand from your question:
Trying to untangle that here my answer:
it is not really best practice to put a lot of code in app.js or index.js so I would try to avoid that as much as possible
sync storage as well as redux are normally just available at runtime and often are loaded from the async storage
async storage needs time and thus also needs to be loaded either with a then promise look or await catch
--> If I was you I would use async storage as normal with await and introduce some sort of splash screen where you do the loading.