Abstraction for exponential and custom retry strategies for failed operations
import pRetried, {
AbortError,
} from "https://deno.land/x/[email protected]/mod.ts"
async function run() {
const response = await fetch("https://sindresorhus.com/unicorn")
// Abort retrying if the resource doesn't exist
if (response.status === 404) {
throw new AbortError(response.statusText)
}
return response.blob()
}
console.log(await pRetried(run, { retries: 5 }))You can pass arguments to the function being retried by wrapping it in an inline arrow function:
import pRetried from "https://deno.land/x/[email protected]/mod.ts"
const run = async emoji => {
// …
}
// Without arguments
await pRetried(run, {
retries: 5,
})
// With arguments
await pRetried(() => run("🦄"), {
retries: 5,
})- HUGE thanks to @sindresorhus -- this repository is mostly his code, modified to work with Deno
- Promise Retried is licensed under the MIT license.
- Code is adapted from Sindre's p-retry for node (also under the MIT license)