source: node_modules/es-toolkit/dist/function/retry.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.3 KB
RevLine 
[a762898]1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const delay = require('../promise/delay.js');
6
7const DEFAULT_DELAY = 0;
8const DEFAULT_RETRIES = Number.POSITIVE_INFINITY;
9const DEFAULT_SHOULD_RETRY = () => true;
10async function retry(func, _options) {
11 let delay$1;
12 let retries;
13 let signal;
14 let shouldRetry;
15 if (typeof _options === 'number') {
16 delay$1 = DEFAULT_DELAY;
17 retries = _options;
18 signal = undefined;
19 shouldRetry = DEFAULT_SHOULD_RETRY;
20 }
21 else {
22 delay$1 = _options?.delay ?? DEFAULT_DELAY;
23 retries = _options?.retries ?? DEFAULT_RETRIES;
24 signal = _options?.signal;
25 shouldRetry = _options?.shouldRetry ?? DEFAULT_SHOULD_RETRY;
26 }
27 let error;
28 for (let attempts = 0; attempts < retries; attempts++) {
29 if (signal?.aborted) {
30 throw error ?? new Error(`The retry operation was aborted due to an abort signal.`);
31 }
32 try {
33 return await func();
34 }
35 catch (err) {
36 error = err;
37 if (!shouldRetry(err, attempts)) {
38 throw err;
39 }
40 const currentDelay = typeof delay$1 === 'function' ? delay$1(attempts) : delay$1;
41 await delay.delay(currentDelay);
42 }
43 }
44 throw error;
45}
46
47exports.retry = retry;
Note: See TracBrowser for help on using the repository browser.