I am attempting to fetch JSON data on multiple URLs. The URL is a single fixed string with unique keys concatenated at the end. These keys are supplied as an array which I'm iterating over using map(). The resulting data is passed into Promise.all(), but undefined is returned. I can't for the life of me figure out why?
export const fetchApiEvents = async eventIds => {
try {
const events = await Promise.all(eventIds.map(id => {
fetch('http://api_url/events/' + id)
.then(res => res.json())
}))
return events
} catch (e) {
console.log('An error occured fetching the events: ', e)
}
}
Then I'm calling this function as an effect:
useEffect(() => {
fetchApiEvents(eventIds)
.then(response => setEvents(response))
}, [eventIds])
Am I doing something really stupid?