I read about await
stop the execution while the promise don't get resolve. I'm trying to do the following in my React-native app:
static async getFromStorage(key) {
const value = await AsyncStorage.getItem(key);
console.log(value);
return value;
}
console.log(Class.getFromStorage("test"));
But I get the following instead of the real value (Chrome didn't allow me to copy the timestamp...):
- Promise {_45: 0, _81: 0, _65: null, _54: null} <- this is the console.log after the function
- 100ms later
- REAL_VALUE <- this is the console.log from the function
So why my code not waiting for resolve the promise?
UPDATE
@thedude suggest:
async function waitForStorage() {
console.log(await getFromStorage());
}
waitForStorage();
The problem with this solution I need it in the class what I want to use. but when I do someVar = await getFromStorage()
, I get a 500 error. I can wait for it, because it is 100ms.
For example:
someVar = await getFromStorage("token");
checkToken(someVar);