1. 次のコードは、リトライ戦略が単純で、ネットワークが一時的に不安定な場合と恒久的に落ちている場合を区別していません。非機能要件(信頼性)の観点から、より良い改善はどれでしょうか?
async function fetchWithRetry(url) {
for (let i = 0; i < 2; i++) {
try {
const res = await fetch(url);
if (!res.ok) throw new Error('bad status');
return res;
} catch (e) {
await new Promise(r => setTimeout(r, 100));
}
}
throw new Error('failed after 2 retries');
}
async function fetchWithRetry(url) {
for (let i = 0; i < 2; i++) {
try {
const res = await fetch(url);
if (!res.ok) throw new Error('bad status');
return res;
} catch (e) {
await new Promise(r => setTimeout(r, 100));
}
}
throw new Error('failed after 2 retries');
}