26. ES6 Promise 简介
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> // 无法请求谁先谁后 // let user; // $.get('https://api.github.com/users',data=>{ // console.log('fetched all user'); // user=data[0].login; // }) // $.get(`https://api.github.com/users/${user}/repos`,data=>{ // console.log('fetched user repos'); // console.log(data); // }) // 为了确保顺序就会把第二个请求放到第一个 // let user; // $.get('https://api.github.com/users',data=>{ // console.log('fetched all user'); // user=data[0].login; // $.get(`https://api.github.com/users/${user}/repos`,data=>{ // console.log('fetched user repos'); // console.log(data); // }) // }) // 为了避免回调地狱,解决方案使用Promise,不管如何都会返回一个结果 </script> </body> </html>
axios
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> --> <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.16.2/axios.min.js"></script> <script> // 为了避免回调地狱,解决方案使用Promise,不管如何都会返回一个结果 let username; // 这里返回一个Promise const users= axios.get('https://api.github.com/users'); //then相当jquery的监听事件 users.then(response=>{ username = response.data[0].login; return axios.get(`https://api.github.com/users/${username}/repos`); }).then(reponse=>{ console.log(reponse.data); }).catch(err=>{ console.log(err); }) </script> </body> </html>
标签:ES6,console,log,笔记,api,user,简单,data,users From: https://www.cnblogs.com/hechunfeng/p/17173661.html