for循环axios-异步解决方案

    <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>
posted @ 2022-12-30 07:19  Felix_Openmind  阅读(164)  评论(0)    收藏  举报
*{cursor: url(https://files-cdn.cnblogs.com/files/morango/fish-cursor.ico),auto;}