Skip to content

Commit

Permalink
Skip delay for failures in last try (#86)
Browse files Browse the repository at this point in the history
Co-authored-by: kali <[email protected]>
  • Loading branch information
Kanggg and kali authored Feb 16, 2023
1 parent 6a58d46 commit 1d8167b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blend-promise-utils",
"version": "1.29.0",
"version": "1.29.1",
"author": "Blend",
"license": "MIT",
"homepage": "https://blend.github.io/promise-utils",
Expand Down
2 changes: 1 addition & 1 deletion src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function retry<T extends Function>(fn: T, retryOpts: RetryOpts): T {
}
lastErr = err;
}
if (retryOpts.delayMs) {
if (retryOpts.delayMs && i < retryOpts.maxAttempts - 1) {
await delay(retryOpts.delayMs);
}
}
Expand Down
8 changes: 7 additions & 1 deletion test/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ import * as promiseUtils from '../src/index';

const sandbox = sinon.createSandbox();

test.afterEach(() => sandbox.restore());

test('fails eventually', async t => {
const maxAttempts = 3;
const delayStub = sandbox.stub(delay, 'delay');

await t.throwsAsync(
promiseUtils.retry(
() => {
throw new Error('testing failures');
},
{ maxAttempts: 3 },
{ maxAttempts: 3, delayMs: 100 },
),
/testing failure/,
);
t.is(delayStub.callCount, maxAttempts - 1);
});

test('honors immediate failure scenarios', async t => {
Expand Down

0 comments on commit 1d8167b

Please sign in to comment.