首页 > 其他分享 >for循环axios-异步解决方案

for循环axios-异步解决方案

时间:2022-12-30 07:33:28浏览次数:46  
标签:异步 axios const nameList res reject 解决方案 axiosPromises

    <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

相关文章