Comparing version 4.1.0 to 4.2.0
@@ -24,4 +24,24 @@ import {OperationOptions} from 'retry'; | ||
Callback invoked on each retry. Receives the error thrown by `input` as the first argument with properties `attemptNumber` and `retriesLeft` which indicate the current attempt number and the number of attempts left, respectively. | ||
The `onFailedAttempt` function can return a promise. For example, to add a [delay](https://github.com/sindresorhus/delay): | ||
``` | ||
import pRetry = require('p-retry'); | ||
import delay = require('delay'); | ||
const run = async () => { ... }; | ||
(async () => { | ||
const result = await pRetry(run, { | ||
onFailedAttempt: async error => { | ||
console.log('Waiting for 1 second before retrying'); | ||
await delay(1000); | ||
} | ||
}); | ||
})(); | ||
``` | ||
If the `onFailedAttempt` function throws, all retries will be aborted and the original promise will reject with the thrown error. | ||
*/ | ||
readonly onFailedAttempt?: (error: FailedAttemptError) => void; | ||
readonly onFailedAttempt?: (error: FailedAttemptError) => void | Promise<void>; | ||
} | ||
@@ -28,0 +48,0 @@ |
@@ -56,4 +56,10 @@ 'use strict'; | ||
decorateErrorWithCounts(error, attemptNumber, options); | ||
options.onFailedAttempt(error); | ||
try { | ||
await options.onFailedAttempt(error); | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (!operation.retry(error)) { | ||
@@ -60,0 +66,0 @@ reject(operation.mainError()); |
{ | ||
"name": "p-retry", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "Retry a promise-returning or async function", | ||
@@ -46,7 +46,7 @@ "license": "MIT", | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"ava": "^2.4.0", | ||
"delay": "^4.1.0", | ||
"tsd": "^0.7.1", | ||
"xo": "^0.24.0" | ||
"tsd": "^0.10.0", | ||
"xo": "^0.25.3" | ||
} | ||
} |
@@ -37,4 +37,28 @@ # p-retry [![Build Status](https://travis-ci.org/sindresorhus/p-retry.svg?branch=master)](https://travis-ci.org/sindresorhus/p-retry) | ||
With the `onFailedAttempt` option: | ||
## API | ||
### pRetry(input, options?) | ||
Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled promise. If calling `input` returns a rejected promise, `input` is called again until the maximum number of retries is reached. It then rejects with the last rejection reason. | ||
It doesn't retry on `TypeError` as that's a user error. | ||
#### input | ||
Type: `Function` | ||
Receives the current attempt number as the first argument and is expected to return a `Promise` or any value. | ||
#### options | ||
Type: `object` | ||
Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module. | ||
##### onFailedAttempt(error) | ||
Type: `Function` | ||
Callback invoked on each retry. Receives the error thrown by `input` as the first argument with properties `attemptNumber` and `retriesLeft` which indicate the current attempt number and the number of attempts left, respectively. | ||
```js | ||
@@ -66,31 +90,25 @@ const run = async () => { | ||
The `onFailedAttempt` function can return a promise. For example, to add a [delay](https://github.com/sindresorhus/delay): | ||
## API | ||
```js | ||
const pRetry = require('p-retry'); | ||
const delay = require('delay'); | ||
### pRetry(input, [options]) | ||
const run = async () => { ... }; | ||
Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled promise. If calling `input` returns a rejected promise, `input` is called again until the max retries are reached, it then rejects with the last rejection reason. | ||
(async () => { | ||
const result = await pRetry(run, { | ||
onFailedAttempt: async error => { | ||
console.log('Waiting for 1 second before retrying'); | ||
await delay(1000); | ||
} | ||
}); | ||
})(); | ||
``` | ||
It doesn't retry on `TypeError` as that's a user error. | ||
If the `onFailedAttempt` function throws, all retries will be aborted and the original promise will reject with the thrown error. | ||
#### input | ||
### pRetry.AbortError(message) | ||
### pRetry.AbortError(error) | ||
Type: `Function` | ||
Receives the number of attempts as the first argument and is expected to return a `Promise` or any value. | ||
#### options | ||
Type: `Object` | ||
Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module. | ||
##### onFailedAttempt(error) | ||
Type: `Function` | ||
Callback invoked on each retry. Receives the error thrown by `input` as the first argument with properties `attemptNumber` and `retriesLeft` which indicate the current attempt number and the number of attempts left, respectively. | ||
### pRetry.AbortError(message|error) | ||
Abort retrying and reject the promise. | ||
@@ -136,6 +154,1 @@ | ||
- [More…](https://github.com/sindresorhus/promise-fun) | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
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
10232
144
152