summaryrefslogtreecommitdiffstats
path: root/src/utils.ts
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2024-03-13 21:21:05 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2024-03-14 17:20:01 +0200
commitc890ea83d6e49702f0e3201b29c472f095193133 (patch)
treefc95bc2502b468a12a8e4a574fd8e3ca5021df5a /src/utils.ts
parent0dda3ef940ce63196df949a8e31712b15ce35cf7 (diff)
Initial commit
Diffstat (limited to 'src/utils.ts')
-rw-r--r--src/utils.ts23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/utils.ts b/src/utils.ts
new file mode 100644
index 0000000..b9b5cfa
--- /dev/null
+++ b/src/utils.ts
@@ -0,0 +1,23 @@
+export const wait = (ms: number) =>
+ new Promise((resolve) => setTimeout(resolve, ms));
+
+export const runWithRetries = async <T>(
+ fn: () => Promise<T>,
+ retries: number,
+ backoff: number = 2000
+): Promise<T> => {
+ let attempts = 0;
+ while (true) {
+ await wait(attempts * backoff);
+ try {
+ return await fn();
+ } catch (e) {
+ if (attempts >= retries) {
+ throw e;
+ }
+
+ console.error("Error:", e);
+ attempts++;
+ }
+ }
+};