异步数据顺序执行问题

<script>
            let URL = 'https://api.github.com/users/a';
            let list_pro = [];
            // promise_All处理异步函数顺序
            for(let i=0;i<10;i++){
                let url=URL+i;
                list_pro.push(fetch(url).then(   //push进去的为promise对象! 
                    res=>res.json()
                ).then(res=>{console.log(res);
                return res.login}));
            }
            console.log(list_pro);
            // 全部执行完之后再执行……
            Promise.all(list_pro).then(res=>{       //拿到的为18行return 出来的结果
                console.log(`执行完所有才OK,${res}`);
            });
            // 执行完一个后就执行……
            Promise.race(list_pro).then(res=>{
                console.log(`执行完第一个就OK,${res}`);     //返回值为第一个执行的数据
            });
            
            // // async异步函数
            async function getlist(){
                let resList=[];
                for(let i=0;i<10;i++){
                    let url = URL+i;
                   let details = await fetch(url).then(res=>res.json());  //返回值为等待过后的值(非promise对象!)
                   console.log(details);
                   resList.push(details);
                }
                console.log("全部加载完啦,async");
                return resList;
            }
            getlist().then(res=>{console.log(res)});
        </script>

猜你喜欢

转载自blog.csdn.net/William526/article/details/86700994
今日推荐