zoukankan      html  css  js  c++  java
  • axios 设置接口retry次数与间隔时间

    /设置全局的请求次数,请求的间隙
    axios.defaults.retry = 3;
    axios.defaults.retryDelay = 2000;
    
    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);
        });
    });
  • 相关阅读:
    八、分组
    七、select使用
    六、SQL基础应用
    五、修改MySQL密码
    side Effect
    js函数式编程
    React生命周期
    Portals
    git使用技巧
    函数式编程
  • 原文地址:https://www.cnblogs.com/echolife/p/11390141.html
Copyright © 2011-2022 走看看