//定義一個請求news的方法 function getNews(url) { //建立一個promise對象 let promise = new Promise((resolve, reject) => { //初始化promise狀態爲pending //啓動異步任務 let request = new XMLHttpRequest(); request.onreadystatechange = function () { if(request.readyState === 4){ if(request.status === 200){ let news = request.response; resolve(news); }else{ reject('請求失敗了。。。'); } } }; request.responseType = 'json';//設置返回的數據類型 request.open("GET", url);//規定請求的方法,建立連接 request.send();//發送 }) return promise; } getNews('http://localhost:3000/news?id=2') .then((news) => { console.log(news); document.write(JSON.stringify(news)); console.log('http://localhost:3000' + news.commentsUrl); return getNews('http://localhost:3000' + news.commentsUrl); }, (error) => { alert(error); }) .then((comments) => { console.log(comments); document.write('<br><br><br><br><br>' + JSON.stringify(comments)); }, (error) => { alert(error); })