diff --git a/.dist.babelrc b/.dist.babelrc index a5c452249..e58cd45c3 100644 --- a/.dist.babelrc +++ b/.dist.babelrc @@ -6,5 +6,7 @@ } }] ], - "sourceMaps": "inline" + "plugins": [ + ["@babel/plugin-transform-runtime"] + ] } diff --git a/.dist.eslintrc b/.dist.eslintrc index 428058969..fb9ee24d5 100644 --- a/.dist.eslintrc +++ b/.dist.eslintrc @@ -18,7 +18,8 @@ "no-useless-escape": "off", "no-cond-assign": "off", "no-redeclare": "off", - "node/no-exports-assign": "off" + "no-fallthrough": "off", + "no-constant-condition": "off" }, "globals": { "regeneratorRuntime": "writable" diff --git a/.lib.babelrc b/.lib.babelrc index 1e8c9e5ea..216688ec5 100644 --- a/.lib.babelrc +++ b/.lib.babelrc @@ -7,5 +7,7 @@ } }] ], - "sourceMaps": "inline" + "plugins": [ + ["@babel/plugin-transform-runtime"] + ] } diff --git a/.lib.eslintrc b/.lib.eslintrc index 78f041df8..c46b829e1 100644 --- a/.lib.eslintrc +++ b/.lib.eslintrc @@ -11,7 +11,8 @@ "node/no-unsupported-features/node-builtins": "off", "no-func-assign": "off", "no-global-assign": ["error", {"exceptions": ["exports"]}], - "node/no-exports-assign": "off" + "no-fallthrough": "off", + "no-constant-condition": "off" }, "overrides": [ { diff --git a/docs/index.md b/docs/index.md index 90d7d0356..168d18748 100644 --- a/docs/index.md +++ b/docs/index.md @@ -246,6 +246,15 @@ This method has two optional arguments: number of retries (default 1) and a call .then(finished); .catch(failed); +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: + + .retry(10, async function(err, res) { + let delay = 3000; // ms + log.error(`Request failed, retrying in ${delay / 1000} seconds ...`); + await new Promise(resolve => setTimeout(() => resolve(), delay)); + return true; + }) + Use `.retry()` only with requests that are *idempotent* (i.e. multiple requests reaching the server won't cause undesirable side effects like duplicate purchases). ## Setting Accept diff --git a/package.json b/package.json index f03b0173b..1f639b0ab 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "Nick Baugh " ], "dependencies": { + "@babel/runtime": "7.4.5", "component-emitter": "^1.3.0", "cookiejar": "^2.1.2", "debug": "^4.1.1", diff --git a/src/client.js b/src/client.js index 419f225aa..3f504d905 100644 --- a/src/client.js +++ b/src/client.js @@ -646,8 +646,8 @@ Request.prototype._getFormData = function() { * @api private */ -Request.prototype.callback = function(err, res) { - if (this._shouldRetry(err, res)) { +Request.prototype.callback = async function(err, res) { + if (err !== null && (await this._shouldRetry(err, res))) { return this._retry(); } diff --git a/src/node/index.js b/src/node/index.js index 62b3c13c1..26e863f0d 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -857,8 +857,8 @@ Request.prototype.request = function() { * @api private */ -Request.prototype.callback = function(err, res) { - if (this._shouldRetry(err, res)) { +Request.prototype.callback = async function(err, res) { + if (err !== null && (await this._shouldRetry(err, res))) { return this._retry(); } diff --git a/src/request-base.js b/src/request-base.js index b88e27cb5..e378d75a4 100644 --- a/src/request-base.js +++ b/src/request-base.js @@ -178,14 +178,14 @@ const ERROR_CODES = ['ECONNRESET', 'ETIMEDOUT', 'EADDRINFO', 'ESOCKETTIMEDOUT']; * @param {Response} [res] response * @returns {Boolean} if segment should be retried */ -RequestBase.prototype._shouldRetry = function(err, res) { +RequestBase.prototype._shouldRetry = async function(err, res) { if (!this._maxRetries || this._retries++ >= this._maxRetries) { return false; } if (this._retryCallback) { try { - const override = this._retryCallback(err, res); + const override = await this._retryCallback(err, res); if (override === true) return true; if (override === false) return false; // undefined falls back to defaults