-3

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);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
PumpkinSeed
  • 2,945
  • 9
  • 36
  • 62
  • Are you using a transpilier such as babel? es6/7 is not supported everywhere. The whole point about async await is to write asynchronous code in a synchronous fashion. Nothing after your await statement should be executed until the promise that is being await is either resolved or rejected. – Kyle Richardson Apr 18 '17 at 15:03
  • @KyleRichardson I wrote I'm using React-native, which is using Babel. Yes, this is what I expect about await as well, but it seems the order of the execution isn't correct. – PumpkinSeed Apr 18 '17 at 15:06
  • My apologizes, I missed the -native part. I'll attempt to figure this out, it should work for you... I use the async await pattern in my nodejs code and it works wonderfully. – Kyle Richardson Apr 18 '17 at 15:07
  • 2
    *"The problem with this solution I need it globally and immediatelly in the class what I want to use"* that is not what async/await solves. Nothing can solve that problem. Use callbacks or make everything async. Even if you used async, it still wouldn't be available immediately and globally. – Kevin B Apr 18 '17 at 15:11
  • @KyleRichardson Yes, this is why I start to do this, because I read about. – PumpkinSeed Apr 18 '17 at 15:11
  • @KevinB basically I just want to use it as the "For example:" part shown. – PumpkinSeed Apr 18 '17 at 15:12
  • *The problem with this solution I need it globally and immediatelly in the class what I want to use* - then you have design problem. It isn't possible to get the result from asynchronous function 'globally and immediatelly'. This is XY problem, and I suggest to ask a new question that reflects the real issue and provides all necessary details. – Estus Flask Apr 18 '17 at 15:12
  • @PumpkinSeed wrap said code in an async function. but now whatever calls that function needs to be async too, or it won't be able to get the value either. You're going down a rabbit hole. **Stop trying to do the impossible and embrace callbacks.** – Kevin B Apr 18 '17 at 15:13
  • @KyleRichardson Please provide a comment why did you downvote me. – PumpkinSeed Apr 18 '17 at 15:18
  • I downvoted the post for lack of research. Don't falsely place blame on people when you have no way of knowing who did it. – Kevin B Apr 18 '17 at 15:19
  • @KevinB: "lack of research": https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await This is the await description: "A Promise or any value to wait for the resolution." basically I implement the same as the examples. – PumpkinSeed Apr 18 '17 at 15:20
  • 1
    @PumpkinSeed I didn't downvote you. I rarely downvote people, I attempt to help them - If I ever downvote someone, I comment to let them know why. People who downvote without comment are trolls in my mind. – Kyle Richardson Apr 18 '17 at 15:21
  • 1
    Except your calling function isn't async. async doesn't just magically make asynchronous code run synchronously; it just hides the callbacks behind promises that you then have to add a callback to. It won't and can't solve your design problem. – Kevin B Apr 18 '17 at 15:21
  • @KevinB My apologizes, but I didn't see that in my researches, but this isn't a reason for downvote. I read whole blog posts about, it isn't my fault there is a lots of crap blog post about. – PumpkinSeed Apr 18 '17 at 15:24
  • Possible duplicate? http://stackoverflow.com/questions/33879401/es6-class-es7-async-await-getter – Kevin B Apr 18 '17 at 15:24
  • @KevinB It is still not what I'm looking for. I just want to get the AsyncStorage item like the localstorage item. – PumpkinSeed Apr 18 '17 at 15:29
  • Right, and that question again says that is impossible. Here's another one: http://stackoverflow.com/questions/41976606/how-to-await-an-async-call-in-javascript-in-a-synchronous-function and another http://stackoverflow.com/questions/42477069/loading-files-synchronously-in-javascript-with-await-operator – Kevin B Apr 18 '17 at 15:29
  • @KevinB In this case I have to look for an other solution for store the data on the device instead of the React Native AsyncStorage. However they suggest use that instead of LocalStorage. – PumpkinSeed Apr 18 '17 at 15:36
  • 1
    FWIW, async functions are part of ES2017, not part of ES6 or ES7. – Felix Kling Apr 18 '17 at 16:50

1 Answers1

1

The code running the console.log should also be marked as async and it should also await the return value of getFromStorage;

e.g. this would work:

async function waitForStorage() {
   console.log(await getFromStorage());
}
waitForStorage(); // now the console messages should be in the correct order

Or you could just wait for the promise's resolution:

getFromStorage("token").then(someVar => checkToken(someVar))
thedude
  • 9,388
  • 1
  • 29
  • 30
  • I updated the post, because this isn't really what I'm looking for. – PumpkinSeed Apr 18 '17 at 15:00
  • @PumpkinSeed what you are looking for is not possible unless you mark the scope calling `await` with `async`. The simplest solution is just to wait on the promise e.g. `getFromStorage("token").then(someVar => checkToken(someVar))` – thedude Apr 18 '17 at 15:11
  • Yes I did it thank you. Can you update the post please? – PumpkinSeed Apr 18 '17 at 21:33
  • Can we also just do like so? `async waitForStorage: () => {}` – Jordan Mar 13 '18 at 18:42