|
Last change
on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.2 KB
|
| Line | |
|---|
| 1 | import { delay } from '../promise/delay.mjs';
|
|---|
| 2 |
|
|---|
| 3 | const DEFAULT_DELAY = 0;
|
|---|
| 4 | const DEFAULT_RETRIES = Number.POSITIVE_INFINITY;
|
|---|
| 5 | const DEFAULT_SHOULD_RETRY = () => true;
|
|---|
| 6 | async 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 |
|
|---|
| 43 | export { retry };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.