async function fetchData() {
const promises = []; // 声明 Promise 对象数组
for (let i = 0; i < 10; i++) { // 循环遍历
const promise = fetch(`/api/data/${i}`).then(response => response.json()); // 请求接口并返回 Promise 对象
promises.push(promise); // 将 Promise 对象推入 Promise 对象数组
}
const result = await Promise.all(promises); // 等待所有 Promise 对象的结果返回,并将结果合并为一个数组
return result;
}
// 调用 fetchData() 方法获取所有数据
fetchData()
.then(result => {
// 处理合并后的数据
console.log(result);
})
.catch(error => {
// 处理错误
console.error(error);
});
};
React处理循环请求接口汇总数据
最新推荐文章于 2025-02-21 14:03:52 发布