以下のように記述すると指定した回数リトライします。
const fetchWithRetry = async (url, options, n) => {
try {
return await fetch(url, options)
} catch (err) {
if (n === 1) throw err
return await fetchWithRetry(url, options, n - 1)
}
}
fetchWithRetry("http://localhost/sample", {}, 5)
.then(res => {
console.log("Success:")
})
.catch(err => {
console.log("Error:", err.message)
})