feat(utils): add retry helper with exponential backoff

Refs: #108
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
nocodeandClaude Opus 5 committed 2026-09-13 12:19:10 +02:00
1 parent 1e4bfba3cf
commit 2c9221c728
1 file changed
+19
+19
View File
@@ -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);
}
}
}