function retry(fn, times, delay) {
let currTimes = 1;
return new Promise(function(resolve, reject) {
function attempt() {
fn().then(resolve).catch(function(err) {
console.log(`第 ${currTimes} 次嘗試`);
if(times === 0) {
reject(err)
} else {
times--;
currTimes++;
setTimeout(attempt(), delay)
}
})
}
attempt()
})
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結