现在有这样一个需求,有四个请求同时发出,如果有某个出错了,如果总次数少于10次,我就重新请求
let num = 0;
let res = void 0;
function reqs() {
let pro1 = new Promise((res, rej) => {
res(fetch('http://localhost:8080'));
});
let pro2 = new Promise((res, rej) => {
res(fetch('http://localhost:8080'));
});
let pro3 = new Promise((res, rej) => {
res(fetch('http://localhost:8080'));
});
let pro4 = new Promise((res, rej) => {
res(fetch('http://localhost:8080'));
});
// Promise.all, 如果有出现, 立马会执行catch,但是其它请求还是会发送
Promise.all([pro1, pro2, pro2, pro4]).then((response)=>{
res = response;
}).catch((err) => {
if(num <= 10) {
num ++;
reqs();
}else {
res = 'error';
}
});
}
reqs();
let count = 0;
let res = void 0;
async function request() {
//throw Error('err');
res = await fetch('http://localhost:8080', {
method: 'GET',
}).then((res) => res).catch((err)=>'error');
console.log(res, count);
while(res === 'error' && count <= 10) {
res = await fetch('http://localhost:8080').then((res) => res).catch((err)=>'error');
count ++;
console.log(res, count);
}
if(count > 10) {
console.log('提醒用户请求多次仍然无果');
return 'server no answer';
}else return res;
}
console.log('加loading效果');
request().then((res) => {console.log('关loading');console.log(res);}).catch((err) => {console.log('关loading');console.log('err');});