forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry-on-error-test.js
43 lines (41 loc) · 1.13 KB
/
retry-on-error-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// [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
}
}
}
}