Utils
With Retry
Simple retry wrapper with exponential backoff.
withRetry
import { withRetry } from '@moeru/std/with-retry'
const run = async () =>
fetch('https://example.com')
.then(res => {
if (!res.ok)
throw new Error(res.statusText)
else
return res.text()
})
const runWithRetry = withRetry(run, { retry: 5 })
console.log(await runWithRetry())exponential backoff
import { withRetry } from '@moeru/std/with-retry'
const run = async () =>
fetch('https://example.com')
.then(res => {
if (!res.ok)
throw new Error(res.statusText)
else
return res.text()
})
const runWithRetry = withRetry(run, {
retry: 5,
retryDelayFactor: 2, // default value
retryDelayMax: 60_000, // enable exponential backoff
})
console.log(await runWithRetry())Remarks
If you need more features (such as jitter, signal), you can try foxts/async-retry.
