axios超时重发
2024-08-24 03:37:16
axios的超时是在response中处理的,所以要在response中添加拦截器:
axios.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
var config = err.config;
// If config does not exist or the retry option is not set, reject
if(!config || !config.retry) return Promise.reject(err); // Set the variable for keeping track of the retry count
config.__retryCount = config.__retryCount || 0; // Check if we've maxed out the total number of retries
if(config.__retryCount >= config.retry) {
// Reject with the error
return Promise.reject(err);
} // Increase the retry count
config.__retryCount += 1; // Create new promise to handle exponential backoff
var backoff = new Promise(function(resolve) {
setTimeout(function() {
resolve();
}, config.retryDelay || 1);
}); // Return the promise in which recalls axios to retry the request
return backoff.then(function() {
return axios(config);
});
});
使用:
axios.get('/some/endpoint', { retry: 5, retryDelay: 1000 })
.then(function(res) {
console.log('success', res.data);
})
.catch(function(err) {
console.log('failed', err);
});
出处:https://github.com/axios/axios/issues/164#issuecomment-327837467
最新文章
- css样式之超出隐藏
- Looper.prepare()和Looper.loop()
- IO(五)----打印流
- Es6 学习笔记
- js中this的问题
- linux常见进程与内核线程
- Android View.onMeasure方法的理解
- Git超级实用使用教程
- WPF中的ControlTemplate(控件模板)(转)
- asp实现关键词不区分大小写搜索并高亮显示
- 使用curl获取网站的http的状态码
- (转)Android中截取当前屏幕图片
- 红豆带你从零学C#系列—Visual Studio工具介绍、下载和安装
- Ubuntu 12.04 Android2.2源码make** /classes-full-debug.jar Error 41错误解决
- POJ 2778 DNA Sequence(AC自动机+矩阵快速幂)
- android奋战的一周
- 【Android 系统开发】 Android 系统启动流程简介
- Kafka-python 客户端导致的 cpu 使用过高,且无法消费消息的问题
- oracle的读写分离实现
- 怎样建立你自己的MASM导入库