- 手动封装一个请求函数,可以设置最大请求次数,请求成功则不再请求,请求失败则继续请求直到超过最大次数(流利说)
import fetch from 'fetch';
function request(url,body,successCb,errCb,maxCount =3){
return fetch(url,body).then(res => successCb(res),err=>{
if(maxCount < =0) return errCb('请求超时')
return request(url,body,successCb,errCb,--maxCount)
})
}
// 用法
request('/api',{methos:'get',headers:{},res => {},err => {}})