Khalid Khalid - 1 month ago 10
Javascript Question

How to fetch data Array in Promise

I call API on my react JS app :

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

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


Then output log :

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


How do I fetch data on Array ?

I need get city name list

Answer

The line response.json() returns a promise that resolves to the parsed JSON.

You can simply write

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