-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);
19
  • 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. Commented Apr 18, 2017 at 15:03
  • 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. Commented Apr 18, 2017 at 15:11
  • 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. Commented Apr 18, 2017 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. Commented Apr 18, 2017 at 15:21
  • 1
    FWIW, async functions are part of ES2017, not part of ES6 or ES7. Commented Apr 18, 2017 at 16:50

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

4 Comments

I updated the post, because this isn't really what I'm looking for.
@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))
Yes I did it thank you. Can you update the post please?
Can we also just do like so? async waitForStorage: () => {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.