From c890ea83d6e49702f0e3201b29c472f095193133 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Wed, 13 Mar 2024 21:21:05 +0200 Subject: Initial commit --- src/utils.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/utils.ts (limited to 'src/utils.ts') 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 ( + fn: () => Promise, + retries: number, + backoff: number = 2000 +): Promise => { + 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++; + } + } +}; -- cgit v1.3