下面是一个简单的示例实现,并未包含对异常情况的处理、超时设置等较复杂的功能:
function fetchWithRetry(url, options, maxRetry = 3) {
return new Promise((resolve, reject) => {
const doFetch = async (attempt) => {
try {
const response = await fetch(url, options);
if (response.ok) {
resolve(response);
} else {
throw new Error("Request failed");
}
} catch (error) {
if (attempt < maxRetry) {
console.log(`Attempt ${attempt + 1} failed. Retrying...`);
doFetch(attempt + 1);
} else {
reject(new Error("Max retries exceeded"));
}
}
};
doFetch(0);
});
}