Mark24
记录灵感、技术、思考
JavaScript编程之多次重试
async function retry(fn, times, ms) {
try {
await fn()
} catch(err) {
// have try once before
if(times-1 <= 0) {
return
}
setTimeout(async () => {
retry(fn, times-1, ms)
}, ms)
}
}
async function hello() {
console.log('run hello')
await new Promise((resolve,reject)=> {
setTimeout(() => {
reject('bad hello')
}, 1000)
})
}
retry(hello, 4, 1000)