source: node_modules/es-toolkit/dist/function/retry.mjs@ ba17441

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

Added visualizations

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