Comparing version 3.0.1 to 4.0.0
41
index.js
@@ -21,3 +21,3 @@ 'use strict'; | ||
function decorateErrorWithCounts(error, attemptNumber, options) { | ||
const decorateErrorWithCounts = (error, attemptNumber, options) => { | ||
// Minus 1 from attemptNumber because the first attempt does not count as a retry | ||
@@ -28,17 +28,23 @@ const retriesLeft = options.retries - (attemptNumber - 1); | ||
error.retriesLeft = retriesLeft; | ||
return error; | ||
} | ||
}; | ||
module.exports = (input, options) => new Promise((resolve, reject) => { | ||
options = Object.assign({ | ||
const pRetry = (input, options) => new Promise((resolve, reject) => { | ||
options = { | ||
onFailedAttempt: () => {}, | ||
retries: 10 | ||
}, options); | ||
retries: 10, | ||
...options | ||
}; | ||
const operation = retry.operation(options); | ||
operation.attempt(attemptNumber => Promise.resolve(attemptNumber) | ||
.then(input) | ||
.then(resolve, error => { | ||
operation.attempt(async attemptNumber => { | ||
try { | ||
resolve(await input(attemptNumber)); | ||
} catch (error) { | ||
if (!(error instanceof Error)) { | ||
reject(new TypeError(`Non-error was thrown: "${error}". You should only throw errors.`)); | ||
return; | ||
} | ||
if (error instanceof AbortError) { | ||
@@ -50,14 +56,17 @@ operation.stop(); | ||
reject(error); | ||
} else if (operation.retry(error)) { | ||
decorateErrorWithCounts(error, attemptNumber, options); | ||
options.onFailedAttempt(error); | ||
} else { | ||
decorateErrorWithCounts(error, attemptNumber, options); | ||
options.onFailedAttempt(error); | ||
reject(operation.mainError()); | ||
if (!operation.retry(error)) { | ||
reject(operation.mainError()); | ||
} | ||
} | ||
}) | ||
); | ||
} | ||
}); | ||
}); | ||
module.exports = pRetry; | ||
module.exports.default = pRetry; | ||
module.exports.AbortError = AbortError; |
{ | ||
"name": "p-retry", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "Retry a promise-returning or async function", | ||
@@ -13,9 +13,10 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -42,9 +43,11 @@ "keywords": [ | ||
"dependencies": { | ||
"@types/retry": "^0.12.0", | ||
"retry": "^0.12.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.1.0", | ||
"ava": "^1.3.1", | ||
"delay": "^4.1.0", | ||
"tsd-check": "^0.3.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8018
5
90
2
4
+ Added@types/retry@^0.12.0
+ Added@types/retry@0.12.5(transitive)