3

I'm using AsyncStorage from react-native to get and store data for my app. I successfully store data via AsyncStorage but when I try to getItem from AsyncStorage it

Promise {_40: 0, _65: 1, _55: "30", _72: null}

How to get "30" value? I tried .json and Promise.resolved to get the data, both not working.

react-native: "0.47.1"
android version: 6
Alan Yong
  • 993
  • 2
  • 12
  • 25
  • Use Promise.all(), refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – vibhor1997a Dec 10 '17 at 07:20

3 Answers3

4

if you want to get value from it you must do this :

you have 2 ways :

1.using async/await as code below :

async function(){
  var a = await someFunction(your_input);
  console.log(a)
}

2.using .then()

someFuntion(your_input).then((result)=>{
  console.log(result)
})
sina
  • 41
  • 1
2

this is solution:

    var data=''
    Promise.resolve(your_promise).then(value=>{
    console.log('value:',value)
    data=value;
    }) 
0

You can use:

var myPromise = Promise.resolve({_40: 0, _65: 1, _55: "30", _72: null});
myPromise.then(
  value=>{
    console.log("get value._55",value._55);
  }
)

More on why promises are used and some info on how to use them can be found here.

HMR
  • 37,593
  • 24
  • 91
  • 160