我正在尝试在多个URL上获取JSON数据。 URL是一个固定的字符串,在其末尾连接了唯一的键。这些键作为数组提供,我正在使用map()进行迭代。结果数据传递到Promise.all(),但返回未定义。我无法为自己的生命找出原因吗?
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)
}
}
然后我将此函数称为效果:
useEffect(() => {
fetchApiEvents(eventIds)
.then(response => setEvents(response))
}, [eventIds])
我做的事真的很愚蠢吗?
return
在第4行中丢失:
export const fetchApiEvents = async eventIds => {
try {
const events = await Promise.all(eventIds.map(id => {
return fetch('http://api_url/events/' + id)
.then(res => res.json())
}))
return events
} catch (e) {
console.log('An error occured fetching the events: ', e)
}
}