question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using async storage without return promise

See original GitHub issue

I have using AsyncStorage with redux initial state my question is how to return value without promise Here is my code

const getStorageData = async state => {
  try {
    const value = await AsyncStorage.getItem('setting');
    if (value !== null) {
      return await JSON.parse(value);
    }
  } catch (error) {}
  return state;
};

const defaultState = {
  locale: 'en',
  language: [
    { key: 'en', text: 'English' },
    { key: 'ar', text: 'Arabic' }
  ]
};

initialState

const initialState = { 
  setting: getStorageData(defaultState)
};

the return value is Promise Object , i need return value is raw Object i need return initState without Promise.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
krizzucommented, Apr 2, 2020

AsyncStorage is by nature asynchronous, so you can either use Promise or Callback to retrieve/save value. Here’s API doc with example on how to use it

2reactions
krizzucommented, Mar 3, 2020

@duongkhoangiam

const initialState = { 
  setting: await getStorageData(defaultState) // you need to await for the value, since `getStorageData` is async
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native AsyncStorage returns promise instead of value
the thing is await turns the promise into a value, you don't need to use .then() . Try the following: const keys =...
Read more >
API | Async Storage - GitHub Pages
Returns : Promise resolving with a string value, if entry exists for given key , or null otherwise. Promise can also be rejected...
Read more >
AsyncStorage
On Android, AsyncStorage will use either RocksDB or SQLite based on what is available. ... Each method in the API returns a Promise...
Read more >
AsyncStorage | React Native By Example
If you haven't yet been introduced to asynchronous JavaScript, this means the methods of this storage system can run concurrently with the rest...
Read more >
Async Storage Example
Example of React Native Async Storage. ... getItem('any_key_here').then( (value) => //AsyncStorage returns a promise so adding a callback to get the value ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found