在promise数组中获取价值

问题描述 投票:0回答:3

我正在使用来自react-native的AsyncStorage来获取和存储我的应用程序的数据。我通过AsyncStorage成功存储数据,但是当我尝试从AsyncStorage获取getItem时

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

如何获得“30”的价值?我试过.json和Promise.resolved来获取数据,两者都不起作用。

react-native: "0.47.1"
android version: 6
javascript arrays react-native promise asyncstorage
3个回答
0
投票

如果你想从中获得价值,你必须这样做:

你有两种方式:

1.使用async/await代码如下:

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

2.使用.then()

someFuntion(your_input).then((result)=>{
  console.log(result)
})

0
投票

您可以使用:

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

更多关于为什么使用promises以及如何使用它们的一些信息可以找到here


0
投票

这是解决方案:

    var data=''
    Promise.resolve(your_promise).then(value=>{
    console.log('value:',value)
    data=value;
    }) 
© www.soinside.com 2019 - 2024. All rights reserved.