1
0
mirror of synced 2025-12-20 10:28:40 -05:00
Files
docs/script/helpers/retry-on-error-test.js
2022-10-03 18:38:32 +00:00

44 lines
1.1 KiB
JavaScript

// [start-readme]
//
// Return a function that you can use to run any code within and if it
// throws you get a chance to say whether to sleep + retry.
// Example:
//
// async function mainFunction() {
// if (Math.random() > 0.9) throw new Error('too large')
// return 'OK'
// }
//
// const errorTest = (err) => err instanceof Error && err.message.includes('too large')
// const config = { // all optional
// attempts: 3,
// sleepTime: 800,
// onError: (err, attempts) => console.warn(`Failed ${attempts} attempts`)
// }
// const ok = await retry(errorTest, mainFunction, config)
//
//
// [end-readme]
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
export async function retryOnErrorTest(
errorTest,
callback,
{ attempts = 10, sleepTime = 1000, onError = () => {} } = {}
) {
while (--attempts) {
try {
return await callback()
} catch (error) {
if (errorTest(error)) {
// console.warn('Sleeping on', error.message, { attempts })
if (onError) onError(error, attempts)
await sleep(sleepTime)
} else {
throw error
}
}
}
}