summaryrefslogtreecommitdiff
path: root/src/lib/utils/retry.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-04-30 09:38:48 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-04-30 09:38:48 -0700
commitbd9fcb53a59b6ad4de0ebb83c0329f312d647b11 (patch)
treef814cb867f0ccb6cb7cc6b0a9ff8882840728bb3 /src/lib/utils/retry.ts
parent115ca3bbf2b3e1ee6f46956ef111981bb04c3bf0 (diff)
downloadmistymountainstherapy-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.ts57
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
+ );
+ }
+};