I am trying to check the progress of a listening session stored in AsyncStorage on React Native. My aim is to show progress (stored in a form of a number) against total lines (number).
I have this working code that outputs the total lines (sourced from a different API using fetch, not the problem here), but I can't seem to get the variable i declared outside of async function to be updated inside of the .then block, to be used in the return statment below.
Here's the (half)working code:
....//other existing codes in a React functional component
//this is the async code that checks the store data in AsyncStorage
const checkProgressListening = async(name) => {
let response = await AsyncStorage.getItem(name+'-LISTENING_SESSION')
let json = await JSON.parse(response)
return json;
}
//this is the code that will be rendered in RNUILIB GridList
const renderItemModule = ({item, index}) => {
//declaring a variable outside of the .then function block below
let progress = null;
checkProgressListening(item.englishName).then(async (data) => {
if(data != null){
progress = data.number
}
}).catch((error) => {
console.log(error)
})
return (
<Text>{progress}/{totalnumberoflines}</Text>
)
}
...//other existing code
return(
<GriddList
...
data={dataItems}
renderItem={renderItemModule}
/>
)
I tried to re-assign the variable progress
with a new value from inside of the .then async block, but unsuccessful so far.
I've referred to this article on StackOverflow, but still couldn't get it working Set value of global variable inside async function or inside promise?