Comparing version 1.0.0 to 2.0.0
35
index.js
@@ -6,7 +6,7 @@ 'use strict'; | ||
constructor(message) { | ||
super(message); | ||
super(); | ||
if (message instanceof Error) { | ||
this.originalError = message; | ||
message = message.message; | ||
({message} = message); | ||
} else { | ||
@@ -22,16 +22,27 @@ this.originalError = new Error(message); | ||
module.exports = (input, opts) => new Promise((resolve, reject) => { | ||
const operation = retry.operation(opts); | ||
module.exports = (input, options) => new Promise((resolve, reject) => { | ||
options = Object.assign({ | ||
onFailedAttempt: () => {}, | ||
retries: 10 | ||
}, options); | ||
operation.attempt(attempts => { | ||
Promise.resolve(attempts) | ||
const operation = retry.operation(options); | ||
operation.attempt(attemptNumber => { | ||
const attemptsLeft = options.retries - attemptNumber; | ||
return Promise.resolve(attemptNumber) | ||
.then(input) | ||
.then(resolve, err => { | ||
if (err instanceof AbortError) { | ||
.then(resolve, error => { | ||
if (error instanceof AbortError) { | ||
operation.stop(); | ||
reject(err.originalError); | ||
} else if (err instanceof TypeError) { | ||
reject(error.originalError); | ||
} else if (error instanceof TypeError) { | ||
operation.stop(); | ||
reject(err); | ||
} else if (!operation.retry(err)) { | ||
reject(error); | ||
} else if (operation.retry(error)) { | ||
error.attemptNumber = attemptNumber; | ||
error.attemptsLeft = attemptsLeft; | ||
options.onFailedAttempt(error); | ||
} else { | ||
reject(operation.mainError()); | ||
@@ -38,0 +49,0 @@ } |
{ | ||
"name": "p-retry", | ||
"version": "1.0.0", | ||
"description": "Retry a promise-returning or async function", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-retry", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"retry", | ||
"retries", | ||
"operation", | ||
"failed", | ||
"rejected", | ||
"try", | ||
"exponential", | ||
"backoff", | ||
"attempt", | ||
"async", | ||
"await", | ||
"promises", | ||
"concurrently", | ||
"concurrency", | ||
"parallel", | ||
"bluebird" | ||
], | ||
"dependencies": { | ||
"retry": "^0.10.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"delay": "^1.3.1", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
"name": "p-retry", | ||
"version": "2.0.0", | ||
"description": "Retry a promise-returning or async function", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-retry", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"retry", | ||
"retries", | ||
"operation", | ||
"failed", | ||
"rejected", | ||
"try", | ||
"exponential", | ||
"backoff", | ||
"attempt", | ||
"async", | ||
"await", | ||
"promises", | ||
"concurrently", | ||
"concurrency", | ||
"parallel", | ||
"bluebird" | ||
], | ||
"dependencies": { | ||
"retry": "^0.12.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"delay": "^2.0.0", | ||
"xo": "*" | ||
} | ||
} |
@@ -5,7 +5,9 @@ # p-retry [![Build Status](https://travis-ci.org/sindresorhus/p-retry.svg?branch=master)](https://travis-ci.org/sindresorhus/p-retry) | ||
It does exponential backoff and supports custom retry strategies for failed operations. | ||
## Install | ||
``` | ||
$ npm install --save p-retry | ||
$ npm install p-retry | ||
``` | ||
@@ -22,3 +24,3 @@ | ||
.then(response => { | ||
// abort retrying if the resource doesn't exist | ||
// Abort retrying if the resource doesn't exist | ||
if (response.status === 404) { | ||
@@ -34,3 +36,26 @@ throw new pRetry.AbortError(response.statusText); | ||
With the `onFailedAttempt` option: | ||
```js | ||
const run = () => fetch('https://sindresorhus.com/unicorn') | ||
.then(response => { | ||
if (response.status !== 200) { | ||
throw new Error(response.statusText); | ||
} | ||
return response.json(); | ||
}); | ||
pRetry(run, { | ||
onFailedAttempt: error => { | ||
console.log(`Attempt ${error.attemptNumber} failed. There are ${error.attemptsLeft} attempts left.`), | ||
// 1st request => Attempt 1 failed. There are 4 retries left. | ||
// 2nd request => Attempt 2 failed. There are 3 retries left. | ||
// ... | ||
}, | ||
retries: 5 | ||
}).then(result => {}); | ||
``` | ||
## API | ||
@@ -56,2 +81,8 @@ | ||
##### onFailedAttempt(err) | ||
Type: `Function` | ||
Callback invoked on each retry. Receives the error thrown by `input` as the first argument with properties `attemptNumber` and `attemptsLeft` which indicate the current attempt number and the number of attempts left, respectively. | ||
### pRetry.AbortError(message|error) | ||
@@ -58,0 +89,0 @@ |
Sorry, the diff of this file is not supported yet
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
5629
44
110
+ Addedretry@0.12.0(transitive)
- Removedretry@0.10.1(transitive)
Updatedretry@^0.12.0