<script>
const res = [];
const axiosPromises = [];
const nameList = ["Wangzz", "Wangyt", "Felix"];
for (let i of nameList) {
axiosPromises.push(
new Promise((resolve, reject) => {
axios.get(
`https://api.agify.io/?name=${i}`
)
}).then((res) => {
resolve(res);
}, (err) => {
reject(err);
})
)
}
Promise.all(axiosPromises).then((res) => {
res.forEach((item, index) => {
res.push(item.data.age); // [20, 38, 14]
})
})
</script>
标签:异步,axios,const,nameList,res,reject,解决方案,axiosPromises
From: https://www.cnblogs.com/openmind-ink/p/17013992.html