From 4205ed5d5b8947cc65c28843e8cc332a1128df36 Mon Sep 17 00:00:00 2001 From: nocode Date: Sun, 13 Sep 2026 12:19:10 +0200 Subject: [PATCH] feat(utils): add retry helper with exponential backoff Refs: #108 --- src/utils/retry.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/utils/retry.js diff --git a/src/utils/retry.js b/src/utils/retry.js new file mode 100644 index 0000000..5b56f0a --- /dev/null +++ b/src/utils/retry.js @@ -0,0 +1,19 @@ +const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + +/** + * Retries an async operation with exponential backoff (M13-T05). + * `shouldRetry` decides whether an error is transient. + */ +export async function withRetry( + operation, + { retries = 2, baseDelayMs = 500, shouldRetry = () => true } = {}, +) { + for (let attempt = 0; ; attempt += 1) { + try { + return await operation(attempt); + } catch (error) { + if (attempt >= retries || !shouldRetry(error)) throw error; + await sleep(baseDelayMs * 2 ** attempt); + } + } +}