Comparing version 5.0.0 to 5.1.0
@@ -43,2 +43,29 @@ import {OperationOptions} from 'retry'; | ||
readonly onFailedAttempt?: (error: FailedAttemptError) => void | Promise<void>; | ||
/** | ||
You can abort retrying using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). | ||
When `AbortController.abort(reason)` is called, the promise will be rejected with `reason` as the error message. | ||
*Requires Node.js 16 or later.* | ||
``` | ||
import pRetry from 'p-retry'; | ||
const run = async () => { … }; | ||
const controller = new AbortController(); | ||
cancelButton.addEventListener('click', () => { | ||
controller.abort('User clicked cancel button'); | ||
}); | ||
try { | ||
await pRetry(run, {signal: controller.signal}); | ||
} catch (error) { | ||
console.log(error.message); | ||
//=> 'User clicked cancel button' | ||
} | ||
``` | ||
*/ | ||
readonly signal?: AbortSignal; | ||
} | ||
@@ -57,3 +84,3 @@ | ||
``` | ||
import pRetry from 'p-retry'; | ||
import pRetry, {AbortError} from 'p-retry'; | ||
import fetch from 'node-fetch'; | ||
@@ -66,3 +93,3 @@ | ||
if (response.status === 404) { | ||
throw new pRetry.AbortError(response.statusText); | ||
throw new AbortError(response.statusText); | ||
} | ||
@@ -69,0 +96,0 @@ |
18
index.js
@@ -38,6 +38,10 @@ import retry from 'retry'; | ||
const getDOMException = errorMessage => globalThis.DOMException === undefined | ||
? new Error(errorMessage) | ||
: new DOMException(errorMessage); | ||
export default async function pRetry(input, options) { | ||
return new Promise((resolve, reject) => { | ||
options = { | ||
onFailedAttempt: () => {}, | ||
onFailedAttempt() {}, | ||
retries: 10, | ||
@@ -80,3 +84,15 @@ ...options, | ||
}); | ||
if (options.signal && !options.signal.aborted) { | ||
options.signal.addEventListener('abort', () => { | ||
operation.stop(); | ||
const reason = options.signal.reason === undefined | ||
? getDOMException('The operation was aborted.') | ||
: options.signal.reason; | ||
reject(reason instanceof Error ? reason : getDOMException(reason)); | ||
}, { | ||
once: true, | ||
}); | ||
} | ||
}); | ||
} |
{ | ||
"name": "p-retry", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Retry a promise-returning or async function", | ||
@@ -49,7 +49,7 @@ "license": "MIT", | ||
"devDependencies": { | ||
"ava": "^3.15.0", | ||
"ava": "^4.1.0", | ||
"delay": "^5.0.0", | ||
"tsd": "^0.18.0", | ||
"xo": "^0.46.4" | ||
"tsd": "^0.19.1", | ||
"xo": "^0.48.0" | ||
} | ||
} |
@@ -16,3 +16,3 @@ # p-retry | ||
```js | ||
import pRetry from 'p-retry'; | ||
import pRetry, {AbortError} from 'p-retry'; | ||
import fetch from 'node-fetch'; | ||
@@ -25,3 +25,3 @@ | ||
if (response.status === 404) { | ||
throw new pRetry.AbortError(response.statusText); | ||
throw new AbortError(response.statusText); | ||
} | ||
@@ -104,2 +104,30 @@ | ||
##### signal | ||
Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | ||
You can abort retrying using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). | ||
When `AbortController.abort(reason)` is called, the promise will be rejected with `reason` if it's an instance of `Error`, or a `DOMException` with `reason` as its message otherwise. If no reason is provided, the promise will reject with a `DOMException`. | ||
*Requires Node.js 16 or later.* | ||
```js | ||
import pRetry from 'p-retry'; | ||
const run = async () => { … }; | ||
const controller = new AbortController(); | ||
cancelButton.addEventListener('click', () => { | ||
controller.abort('User clicked cancel button'); | ||
}); | ||
try { | ||
await pRetry(run, {signal: controller.signal}); | ||
} catch (error) { | ||
console.log(error.message); | ||
//=> 'User clicked cancel button' | ||
} | ||
``` | ||
### AbortError(message) | ||
@@ -106,0 +134,0 @@ ### AbortError(error) |
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
12718
171
169