Function
retryOnErrorTest
(
errorTest,
callback,
{ attempts = 10, sleepTime = 1000, onError = () => {} } = {}
)
Source from the content-addressed store, hash-verified
| 23 | const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) |
| 24 | |
| 25 | export async function retryOnErrorTest( |
| 26 | errorTest, |
| 27 | callback, |
| 28 | { attempts = 10, sleepTime = 1000, onError = () => {} } = {} |
| 29 | ) { |
| 30 | while (--attempts) { |
| 31 | try { |
| 32 | return await callback() |
| 33 | } catch (error) { |
| 34 | if (errorTest(error)) { |
| 35 | // console.warn('Sleeping on', error.message, { attempts }) |
| 36 | if (onError) onError(error, attempts) |
| 37 | await sleep(sleepTime) |
| 38 | } else { |
| 39 | throw error |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
Tested by
no test coverage detected