axios 发起请求
axios 发请求还是很简单的,那么我们可以直接仿照他来实现一下封装。
axios({ url: 'https://www.lilnong.top/cors/axiosAutoTry', params: 'user=sf', method: 'get' }) .then(console.log) .catch(console.log)
实现自动重试
我直接在 data
上增加一个 __try_count
用于设置重试次数。
因为 Axios 是支持 Promise,所以我们的方法也支持。
axios 如果成功了我们也 resolve。
axios 如果失败了我们先判断次数,然后根据具体的错误,进行重试。
- 407 就是我理解的抽风
- 413 是请求并发太高,为了不占用多少可以加个延时器。
- 503 也是我理解的抽风
ECONNABORTED
很奇怪好好的资源他也不加载就卡住了,所以我设置了 timeout
ECONNRESET
也是一个很奇怪的错误。(既然是 Node,我理解他经常出错误。)
function axiosAutoTry(data){ return new Promise((resolve, reject)=>{ axios(data) .then((data)=>{ resolve(data) }) .catch(error=>{ // 有重试次数 if(typeof data.__try_count == 'number' && data.__try_count>0){ console.error('重试请求', error.message, data) data.__try_count--; if(error.code == 'ECONNABORTED'){ // 中止,超时 return resolve(axiosAutoTry(data)) }else if(error.code == 'ECONNRESET'){ // return resolve(axiosAutoTry(data)) }else{ if(error.response && error.response.status == 407){ // 代理407 return setTimeout(v=>{ resolve(axiosAutoTry(data)) }, 500 + Math.random() * 500) }else if(error.response && error.response.status == 503){ // 服务器异常 return setTimeout(v=>{