@common.js/p-retry 6.1.0 → 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  The [p-retry](https://www.npmjs.com/package/p-retry) package exported as CommonJS modules.
4
4
 
5
- Exported from [p-retry@6.1.0](https://www.npmjs.com/package/p-retry/v/6.1.0) using https://github.com/etienne-martin/common.js.
5
+ Exported from [p-retry@6.2.0](https://www.npmjs.com/package/p-retry/v/6.2.0) using https://github.com/etienne-martin/common.js.
package/index.d.ts CHANGED
@@ -41,6 +41,28 @@ export type Options = {
41
41
  */
42
42
  readonly onFailedAttempt?: (error: FailedAttemptError) => void | Promise<void>;
43
43
 
44
+ /**
45
+ Decide if a retry should occur based on the error. Returning true triggers a retry, false aborts with the error.
46
+
47
+ It is not called for `TypeError` (except network errors) and `AbortError`.
48
+
49
+ @param error - The error thrown by the input function.
50
+
51
+ @example
52
+ ```
53
+ import pRetry from 'p-retry';
54
+
55
+ const run = async () => { … };
56
+
57
+ const result = await pRetry(run, {
58
+ shouldRetry: error => !(error instanceof CustomError);
59
+ });
60
+ ```
61
+
62
+ In the example above, the operation will be retried unless the error is an instance of `CustomError`.
63
+ */
64
+ readonly shouldRetry?: (error: FailedAttemptError) => boolean | Promise<boolean>;
65
+
44
66
  /**
45
67
  You can abort retrying using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
46
68
 
package/index.js CHANGED
@@ -349,7 +349,10 @@ function _pRetry() {
349
349
  new Promise(function(resolve, reject) {
350
350
  options = _objectSpread({
351
351
  onFailedAttempt: function onFailedAttempt() {},
352
- retries: 10
352
+ retries: 10,
353
+ shouldRetry: function() {
354
+ return true;
355
+ }
353
356
  }, options);
354
357
  var operation = _retry.default.operation(options);
355
358
  var abortHandler = function() {
@@ -377,7 +380,7 @@ function _pRetry() {
377
380
  0,
378
381
  2,
379
382
  ,
380
- 7
383
+ 8
381
384
  ]);
382
385
  return [
383
386
  4,
@@ -389,7 +392,7 @@ function _pRetry() {
389
392
  resolve(result);
390
393
  return [
391
394
  3,
392
- 7
395
+ 8
393
396
  ];
394
397
  case 2:
395
398
  error = _state.sent();
@@ -397,9 +400,9 @@ function _pRetry() {
397
400
  case 3:
398
401
  _state.trys.push([
399
402
  3,
400
- 5,
403
+ 6,
401
404
  ,
402
- 6
405
+ 7
403
406
  ]);
404
407
  if (!_instanceof(error, Error)) {
405
408
  throw new TypeError('Non-error was thrown: "'.concat(error, '". You should only throw errors.'));
@@ -410,34 +413,44 @@ function _pRetry() {
410
413
  if (_instanceof(error, TypeError) && !(0, _isNetworkError.default)(error)) {
411
414
  throw error;
412
415
  }
416
+ decorateErrorWithCounts(error, attemptNumber, options);
413
417
  return [
414
418
  4,
415
- options.onFailedAttempt(decorateErrorWithCounts(error, attemptNumber, options))
419
+ options.shouldRetry(error)
416
420
  ];
417
421
  case 4:
422
+ if (!_state.sent()) {
423
+ operation.stop();
424
+ reject(error);
425
+ }
426
+ return [
427
+ 4,
428
+ options.onFailedAttempt(error)
429
+ ];
430
+ case 5:
418
431
  _state.sent();
419
432
  if (!operation.retry(error)) {
420
433
  throw operation.mainError();
421
434
  }
422
435
  return [
423
436
  3,
424
- 6
437
+ 7
425
438
  ];
426
- case 5:
439
+ case 6:
427
440
  finalError = _state.sent();
428
441
  decorateErrorWithCounts(finalError, attemptNumber, options);
429
442
  cleanUp();
430
443
  reject(finalError);
431
444
  return [
432
445
  3,
433
- 6
446
+ 7
434
447
  ];
435
- case 6:
448
+ case 7:
436
449
  return [
437
450
  3,
438
- 7
451
+ 8
439
452
  ];
440
- case 7:
453
+ case 8:
441
454
  return [
442
455
  2
443
456
  ];
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@common.js/p-retry",
3
- "version": "6.1.0",
3
+ "version": "6.2.0",
4
4
  "description": "p-retry package exported as CommonJS modules",
5
5
  "license": "MIT",
6
6
  "repository": "etienne-martin/common.js",
7
7
  "funding": "https://github.com/sponsors/sindresorhus",
8
8
  "type": "commonjs",
9
+ "types": "./index.d.ts",
10
+ "sideEffects": false,
9
11
  "engines": {
10
12
  "node": ">=16.17"
11
13
  },