递归 axios.get() returns 空数组

Recursive axios.get() returns empty array

我正在尝试根据 post ids

的数组递归执行 axios.get() 请求
let returnArray = []
post_ids = [0,1,2,3,4,5]

for (const id of post_ids) {
    axios.get('api/events/' + id)
        .then(response => returnArray = returnArray.concat(response.data))
        .catch(error => {
            console.log('Event not found: ', error)
            return returnArray
        })
}
return returnArray

我知道其中一个 post ID 无效,并且 return 来自 API 的代码 404。

我遇到的问题是 return 数组是空的,而不是所有有效 post id 查询的 JSON 数据(减去无效的)

有什么想法吗?

谢谢

如果您希望它实际上是递归的,您需要一个可以在 .then 中递归调用的 函数 。但这让事情变得比他们需要的更复杂。 await循环中的每个调用如何?

const returnArray = [];
const post_ids = [0,1,2,3,4,5];
for (const id of post_ids) {
    try {
        returnArray.push(await axios.get('api/events/' + id));
    } catch(e) {
        console.log('Event not found: ', id)
    }
}
return returnArray;