-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
note source from axios/axios#164 (comment)
- Loading branch information
Orion.Lin
committed
Apr 8, 2019
0 parents
commit 9097d22
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Memo | ||
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); | ||
}); | ||
}); | ||
#example | ||
axios.get('/some/endpoint', { retry: 5, retryDelay: 1000 }) | ||
.then(function(res) { | ||
console.log('success', res.data); | ||
}) | ||
.catch(function(err) { | ||
console.log('failed', err); | ||
}); |