在這里,我試圖繞過(guò)Promise.Here在第一個(gè)請(qǐng)求時(shí)獲取一組鏈接。在下一個(gè)請(qǐng)求時(shí),我獲取第一個(gè)鏈接的內(nèi)容。但是我想在返回下一個(gè)Promise對(duì)象之前進(jìn)行延遲。所以我使用setTimeout,但是它給了我下面的JSON錯(cuò)誤(without setTimeout() it works just fine)SyntaxError:JSON.parse:JSON數(shù)據(jù)的第1行第1列出現(xiàn)意外字符我想知道為什么會(huì)失敗?let globalObj={};function getLinks(url){ return new Promise(function(resolve,reject){ let http = new XMLHttpRequest(); http.onreadystatechange = function(){ if(http.readyState == 4){ if(http.status == 200){ resolve(http.response); }else{ reject(new Error()); } } } http.open("GET",url,true); http.send(); });}getLinks('links.txt').then(function(links){ let all_links = (JSON.parse(links)); globalObj=all_links; return getLinks(globalObj["one"]+".txt");}).then(function(topic){ writeToBody(topic); setTimeout(function(){ return getLinks(globalObj["two"]+".txt"); // without setTimeout it works fine },1000);});
在承諾鏈上使用setTimeout
SMILET
2019-09-21 15:10:17