diff options
| author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-04-30 09:38:48 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-04-30 09:38:48 -0700 |
| commit | bd9fcb53a59b6ad4de0ebb83c0329f312d647b11 (patch) | |
| tree | f814cb867f0ccb6cb7cc6b0a9ff8882840728bb3 /src/lib/utils/retry.ts | |
| parent | 115ca3bbf2b3e1ee6f46956ef111981bb04c3bf0 (diff) | |
| download | mistymountainstherapy-bd9fcb53a59b6ad4de0ebb83c0329f312d647b11.tar.gz mistymountainstherapy-bd9fcb53a59b6ad4de0ebb83c0329f312d647b11.zip | |
run prettier and fix bug on safari
Diffstat (limited to 'src/lib/utils/retry.ts')
| -rw-r--r-- | src/lib/utils/retry.ts | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/src/lib/utils/retry.ts b/src/lib/utils/retry.ts index 34bc73b..ed78528 100644 --- a/src/lib/utils/retry.ts +++ b/src/lib/utils/retry.ts @@ -9,37 +9,36 @@ export const RETRY_JITTER_MAX = 3_000; const waitFor = (ms: number) => new Promise((res) => setTimeout(res, ms)); const exponentialStrategyWithJitter: RetryStrategyF = (retries: number) => - WAIT_MS * Math.pow(RETRY_EXPONENT, RETRY_EXPONENTIAL_FACTOR * retries) + - RETRY_JITTER_MAX * Math.random(); + WAIT_MS * Math.pow(RETRY_EXPONENT, RETRY_EXPONENTIAL_FACTOR * retries) + + RETRY_JITTER_MAX * Math.random(); export const continueRetryUntilValidation = async <T>( - promiseFn: () => Promise<T> | T | Promise<void> | void, - validationFn: (x: T) => Promise<boolean> | boolean = (x: T) => - Promise.resolve(!!x), - maxRetries = MAX_DEFAULT_RETRY_AMOUNT, - waitTimeStrategy: RetryStrategyF = exponentialStrategyWithJitter, - retries = 0, - lastError: undefined | unknown = undefined + promiseFn: () => Promise<T> | T | Promise<void> | void, + validationFn: (x: T) => Promise<boolean> | boolean = (x: T) => Promise.resolve(!!x), + maxRetries = MAX_DEFAULT_RETRY_AMOUNT, + waitTimeStrategy: RetryStrategyF = exponentialStrategyWithJitter, + retries = 0, + lastError: undefined | unknown = undefined ): Promise<T> => { - if (retries >= maxRetries) { - if (lastError) throw lastError; - throw new Error("Exceeded maximum retry amount"); - } - try { - if (retries) await waitFor(waitTimeStrategy(retries)); + if (retries >= maxRetries) { + if (lastError) throw lastError; + throw new Error('Exceeded maximum retry amount'); + } + try { + if (retries) await waitFor(waitTimeStrategy(retries)); - const res = await promiseFn(); - if (res && (await validationFn(res))) return res; + const res = await promiseFn(); + if (res && (await validationFn(res))) return res; - throw new Error("Validation predicate unsuccessful"); - } catch (e: unknown) { - return continueRetryUntilValidation( - promiseFn, - validationFn, - maxRetries, - waitTimeStrategy, - retries + 1, - e - ); - } -};
\ No newline at end of file + throw new Error('Validation predicate unsuccessful'); + } catch (e: unknown) { + return continueRetryUntilValidation( + promiseFn, + validationFn, + maxRetries, + waitTimeStrategy, + retries + 1, + e + ); + } +}; |
