I try to give out a general solution for retrying a promise. Below is my way and comes an error of "Uncaught (in promise)".
How can I fix this problem?
function tryAtMost(maxRetries, promise) {
let tries = maxRetries
return new Promise(function(resolve, reject) {
promise.then((result) => {
resolve(result)
})
.catch(err => {
if (tries > 0) {
console.log(`tries with ${tries}`)
tryAtMost(--tries, promise);
} else {
reject(err)
}
})
})
}
tryAtMost(3, promise).then(result => {
console.log(result)
})
.catch(err => {
console.log(err)
})