I am having problem with Promise.all function for multiple API calls. I am making multiple api calls and then console.log the result. I am able to see the results when do console.log
within the map
function, but when trying to set the state in order to render and display in UI, the data is not flowing to state. I don't know where exactly the problem is and why data is not flowing to state.
Here is my code.
componentDidMount() {
const promises = data.map(id => {
return new Promise((resolve) => {
fetch(`https://example.com/api/ITems/${id.ID}`)
.then(response => {
return new Promise(() => {
response.json()
.then(response => {
console.log(response); // this console log is showing me data into console, but the console.log below in promise.all section is not showing any data
resolve()
})
.catch((error) => {
console.log(error);
})
})
})
})
})
Promise.all(promises).then(results => {
this.setState({Ids: results.data}); // no data is passing to state
console.log(results); // this console.log does not work. when remove the console.log in loop, i cannot see anything. data is not flowing here
})
.catch((error) => {
console.log(error);
})
}
Thanks everyone.