Skip to content

Commit 62eae78

Browse files
authored
Improve .retry() method functionality with delays and prevent calling on successful request (#1527)
* Enable .retry() method to use an async function as a callback, to allow easily delaying retries by using a Promise and setTimeout. * Prevent .retry() method from being invoked on successful request. Runs only when there's a defined error. * - Update readme with new async functionality of retry() - Make ESLint happy - Add babel plugin so aync/await works on node 6.x
1 parent f8ea331 commit 62eae78

9 files changed

+26
-10
lines changed

.dist.babelrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
}
77
}]
88
],
9-
"sourceMaps": "inline"
9+
"plugins": [
10+
["@babel/plugin-transform-runtime"]
11+
]
1012
}

.dist.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"no-useless-escape": "off",
1919
"no-cond-assign": "off",
2020
"no-redeclare": "off",
21-
"node/no-exports-assign": "off"
21+
"no-fallthrough": "off",
22+
"no-constant-condition": "off"
2223
},
2324
"globals": {
2425
"regeneratorRuntime": "writable"

.lib.babelrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
}
88
}]
99
],
10-
"sourceMaps": "inline"
10+
"plugins": [
11+
["@babel/plugin-transform-runtime"]
12+
]
1113
}

.lib.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"node/no-unsupported-features/node-builtins": "off",
1212
"no-func-assign": "off",
1313
"no-global-assign": ["error", {"exceptions": ["exports"]}],
14-
"node/no-exports-assign": "off"
14+
"no-fallthrough": "off",
15+
"no-constant-condition": "off"
1516
},
1617
"overrides": [
1718
{

docs/index.md

+9
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ This method has two optional arguments: number of retries (default 1) and a call
246246
.then(finished);
247247
.catch(failed);
248248

249+
The callback supplied can be async if you need to await any data or to create a delay before retrying. This code will retry 10 times, with a 3 second delay between each retry. To add a delay:
250+
251+
.retry(10, async function(err, res) {
252+
let delay = 3000; // ms
253+
log.error(`Request failed, retrying in ${delay / 1000} seconds ...`);
254+
await new Promise(resolve => setTimeout(() => resolve(), delay));
255+
return true;
256+
})
257+
249258
Use `.retry()` only with requests that are *idempotent* (i.e. multiple requests reaching the server won't cause undesirable side effects like duplicate purchases).
250259

251260
## Setting Accept

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"Nick Baugh <[email protected]>"
2424
],
2525
"dependencies": {
26+
"@babel/runtime": "7.4.5",
2627
"component-emitter": "^1.3.0",
2728
"cookiejar": "^2.1.2",
2829
"debug": "^4.1.1",

src/client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,8 @@ Request.prototype._getFormData = function() {
646646
* @api private
647647
*/
648648

649-
Request.prototype.callback = function(err, res) {
650-
if (this._shouldRetry(err, res)) {
649+
Request.prototype.callback = async function(err, res) {
650+
if (err !== null && (await this._shouldRetry(err, res))) {
651651
return this._retry();
652652
}
653653

src/node/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,8 @@ Request.prototype.request = function() {
857857
* @api private
858858
*/
859859

860-
Request.prototype.callback = function(err, res) {
861-
if (this._shouldRetry(err, res)) {
860+
Request.prototype.callback = async function(err, res) {
861+
if (err !== null && (await this._shouldRetry(err, res))) {
862862
return this._retry();
863863
}
864864

src/request-base.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ const ERROR_CODES = ['ECONNRESET', 'ETIMEDOUT', 'EADDRINFO', 'ESOCKETTIMEDOUT'];
178178
* @param {Response} [res] response
179179
* @returns {Boolean} if segment should be retried
180180
*/
181-
RequestBase.prototype._shouldRetry = function(err, res) {
181+
RequestBase.prototype._shouldRetry = async function(err, res) {
182182
if (!this._maxRetries || this._retries++ >= this._maxRetries) {
183183
return false;
184184
}
185185

186186
if (this._retryCallback) {
187187
try {
188-
const override = this._retryCallback(err, res);
188+
const override = await this._retryCallback(err, res);
189189
if (override === true) return true;
190190
if (override === false) return false;
191191
// undefined falls back to defaults

0 commit comments

Comments
 (0)