1
I’m using the AsyncStorage
to save the value of some tokens
with the following code:
Asyncstorage.js:
import AsyncStorage from '@react-native-community/async-storage';
setValue = async (key, value) => {
try {
await AsyncStorage.setItem(key, value)
} catch(e) {
console.log(e)
}
console.log('value set')
}
getMyValue = async (key) => {
try {
const value = await AsyncStorage.getItem(key)
console.log(value)
return value
} catch(e) {
console.log(e)
}
App.js:
import store from './AsyncStorage'
const str = new store
setValue('1', 'teste')
console.log(getMyValue('1'))
Only that the function’s Return is happening before the await returns with the data, because in the React Native console is in the following order:
//essa linha é do console.log(getMyValue('1'))
LOG {"_40": 0, "_55": null, "_65": 0, "_72": null}
LOG value set // essa é o console.log('value set')
LOG teste // essa é o console.log(value)
I tried to give Return right when running Asyncstorage.getItem(key) like this:
return await AsyncStorage.getItem(key)
But you make the same mistake.