长轮循:请求数据,待数据返回之后,再次请求
function http(){
let time = parseInt(Math.random()*5)*1000;
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(time);
}, time);
})
}
function init() {
http().then((time)=>{
console.log(time + 's后返回');
init();
})
}
init();
var http = (function () {
let count = 0;
return function () {
count++;
let time = parseInt(Math.random()*5)*1000;
return new Promise((resolve, reject) => {
setTimeout(()=>{
resolve({time, count});
}, time);
});
}
})();
function init(){
http().then((res) => {
console.log('返回第' + res.count + '次' + res.time + 's');
init();
})
}
init();