How to get data from Promise_ object?

时间:2019-04-08 12:53:04

标签: react-native console local-storage asyncstorage

I'm trying to get some data from a Promise_ object, in React Native app, in my AsyncStorage.

This is what I get in the console, in my Promise_ object :

Promise {_40: 0, _65: 0, _55: null, _72: null}
  _40: 0
  _55: {imageId: "1", imageName: "test.png"}
  _65: 1
  _72: null
__proto__: Object

So I don't know how to simply get the data in the _55 and show them in my code, I just want to take "1" and "test.png". And sorry for my bad english. Thanks !

CODE :

This is the code for the set : export const setBadgePicture = async (badgePictureId, badgePictureName) => await AsyncStorage.multiSet([['imageId', badgePictureId],['imageName', badgePictureName]])

and for the get : export const getBadgePicture = async () => { await AsyncStorage.multiGet(['imageId', 'imageName']).then((response) => { tableResponse = { 'imageId' : response[0][1], 'imageName' : response[1][1], } }) return tableResponse }

4 个答案:

答案 0 :(得分:1)

you can do this to wait until your AsyncStorage returns the item.

 AsyncStorage.getItem('YOUR_KEY').then((response)=>{
      const itemVal = response;
})

答案 1 :(得分:0)

AsyncStorage getItem function is an Asynchronous function. That means that if you want to get the return of that "get" function you will have to wait for the response. if you are using AsyncStorage.getItem() function add the reserved word await before the statement and in the function you are calling it put the reserved word async like this way:

async myFunctionWhereImCallingTheGetter () {
  var myData = await AsyncStorage.getItem('theitemIamSearching');
}

答案 2 :(得分:0)

Try then that:

export const getBadgePicture = async () => {
  var response = await AsyncStorage.multiGet(['imageId', 'imageName']);
  var tableResponse = { 'imageId' : response[0][1], 'imageName' : response[1][1], } });
  return tableResponse;
}

答案 3 :(得分:0)

如果避免multiGet怎么办?

export const getBadgePicture = async () => {
  var imageId = await AsyncStorage.getItem('imageId');
  var imageName = await AsyncStorage.getItem('imageName');
  var tableResponse = { 'imageId' : imageId, 'imageName' : imageName };
  return tableResponse;
}