Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ to your target environment.

(default: `0`)

- **`signal`**: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)

When `signal` is `aborted` any further attempts are canceled. Similar to calling `context.abort()` manually.

- **`handleError`**: `(err, context, options) => Promise<void> | void`

`handleError` is a function that will be invoked when an error occurs
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"babel-plugin-istanbul": "^4.1.5",
"chalk": "^2.3.0",
"conventional-changelog-conventionalcommits": "^4.6.1",
"abortcontroller-polyfill": "^1.7.6",
"coveralls": "^3.0.0",
"inquirer": "^6.2.0",
"nyc": "^12.0.1",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface AttemptOptions<T> {
readonly timeout: number;
readonly jitter: boolean;
readonly initialJitter: boolean;
readonly signal: AbortSignal | null;
readonly handleError: HandleError<T> | null;
readonly handleTimeout: HandleTimeout<T> | null;
readonly beforeAttempt: BeforeAttempt<T> | null;
Expand All @@ -46,6 +47,7 @@ function applyDefaults<T> (options?: PartialAttemptOptions<T>): AttemptOptions<T
timeout: (options.timeout === undefined) ? 0 : options.timeout,
jitter: (options.jitter === true),
initialJitter: (options.initialJitter === true),
signal: (options.signal === undefined) ? null : options.signal,
handleError: (options.handleError === undefined) ? null : options.handleError,
handleTimeout: (options.handleTimeout === undefined) ? null : options.handleTimeout,
beforeAttempt: (options.beforeAttempt === undefined) ? null : options.beforeAttempt,
Expand Down Expand Up @@ -131,7 +133,7 @@ export async function retry<T> (
options.beforeAttempt(context, options);
}

if (context.aborted) {
if (context.aborted || (options.signal && options.signal.aborted)) {
const err: any = new Error(`Attempt aborted`);
err.code = 'ATTEMPT_ABORTED';
throw err;
Expand Down
19 changes: 18 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only';
import test from 'ava';
import {
retry, sleep, defaultCalculateDelay,
Expand Down Expand Up @@ -92,7 +93,8 @@ test('should default to 3 attempts with 200 delay', async (t) => {
handleError: null,
handleTimeout: null,
beforeAttempt: null,
calculateDelay: null
calculateDelay: null,
signal: null
});

attemptCount++;
Expand Down Expand Up @@ -530,6 +532,21 @@ test('should allow attempts to be aborted via handleError', async (t) => {
t.is(err.retryable, false);
});

test('should allow attempts to be aborted via AbortSignal', async (t) => {
const contoller = new AbortController();
const err = await t.throws(retry(async (context) => {
if (context.attemptNum === 1) {
contoller.abort();
}
throw new Error('try again');
}, {
delay: 0,
maxAttempts: 4,
signal: contoller.signal
}));
t.is(err.code, 'ATTEMPT_ABORTED');
});

test('should wait for async handleError to resolve before retrying', async (t) => {
let promiseHasResolved = false;
await t.notThrows(retry(async ({ attemptNum }) => {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,11 @@ abbrev@1, abbrev@~1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"

abortcontroller-polyfill@^1.7.6:
version "1.7.6"
resolved "https://registry.yarnpkg.com/abortcontroller-polyfill/-/abortcontroller-polyfill-1.7.6.tgz#7be8d35b5ed7dfa1a51b36f221720b23deb13f36"
integrity sha512-Zypm+LjYdWAzvuypZvDN0smUJrhOurcuBWhhMRBExqVLRvdjp3Z9mASxKyq19K+meZMshwjjy5S0lkm388zE4Q==

agent-base@6, agent-base@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
Expand Down