如何在Promise中获取数据数组

时间:2016-11-01 03:05:20

标签: javascript reactjs promise

我在我的反应JS应用程序上调用API:

result = fetch('http://localhost:5000/cities')

result.then(function(response) {
 console.log(response.json());
})

然后输出日志:

Promise
  __proto__ : Promise
  [[PromiseStatus]] : "resolved"
  [[PromiseValue]] : Array[5]
     0: Object
     1: Object
     2: Object
     3: Object
     4: Object
      length: 5
     __proto__: Array[0]

如何获取Array上的数据?

我需要获取城市名单

1 个答案:

答案 0 :(得分:1)

response.json()返回一个解析为已解析JSON的承诺。

你可以简单地写

result.then(function(response) {
  return response.json();
}).then(function (data) {
  console.log(data);
});