Skip to content

Commit b400af6

Browse files
authoredNov 15, 2024··
Fix onFailedAttempt and shouldRetry options being undefined (#82)
1 parent 21a22dd commit b400af6

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed
 

‎index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ const decorateErrorWithCounts = (error, attemptNumber, options) => {
2929

3030
export default async function pRetry(input, options) {
3131
return new Promise((resolve, reject) => {
32-
options = {
33-
onFailedAttempt() {},
34-
retries: 10,
35-
shouldRetry: () => true,
36-
...options,
37-
};
32+
options = {...options};
33+
options.onFailedAttempt ??= () => {};
34+
options.shouldRetry ??= () => true;
35+
options.retries ??= 10;
3836

3937
const operation = retry.operation(options);
4038

‎test.js

+26
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,32 @@ test('onFailedAttempt can throw, causing all retries to be aborted', async t =>
222222
}
223223
});
224224

225+
test('onFailedAttempt can be undefined', async t => {
226+
const error = new Error('thrown from onFailedAttempt');
227+
228+
await t.throwsAsync(pRetry(() => {
229+
throw error;
230+
}, {
231+
onFailedAttempt: undefined,
232+
retries: 1,
233+
}), {
234+
is: error,
235+
});
236+
});
237+
238+
test('shouldRetry can be undefined', async t => {
239+
const error = new Error('thrown from onFailedAttempt');
240+
241+
await t.throwsAsync(pRetry(() => {
242+
throw error;
243+
}, {
244+
shouldRetry: undefined,
245+
retries: 1,
246+
}), {
247+
is: error,
248+
});
249+
});
250+
225251
test('throws useful error message when non-error is thrown', async t => {
226252
await t.throwsAsync(pRetry(() => {
227253
throw 'foo'; // eslint-disable-line no-throw-literal

0 commit comments

Comments
 (0)
Please sign in to comment.