-
Notifications
You must be signed in to change notification settings - Fork 32
/
async_retry.html
64 lines (57 loc) · 1.56 KB
/
async_retry.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Retrying an Async Operation on Failure</title>
<style>
button {
margin-top: 10px;
}
pre {
background-color: #f7f7f7;
padding: 10px;
}
</style>
</head>
<body>
<h1>Retrying an Async Operation on Failure</h1>
<button id="retryButton">Retry</button>
<pre id="output"></pre>
<script>
async function makeRequest(url) {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
throw error;
}
}
async function retry(fn, maxRetries = 3, retryDelay = 1000) {
let retries = 0;
while (retries <= maxRetries) {
try {
return await fn();
} catch (error) {
retries++;
console.error(error);
await new Promise(resolve => setTimeout(resolve, retryDelay));
}
}
throw new Error(`Failed after ${retries} retries.`);
}
const retryButton = document.getElementById('retryButton');
const output = document.getElementById('output');
retryButton.addEventListener('click', async () => {
output.innerText = '';
try {
const data = await retry(() => makeRequest('https://jsonplaceholder.typicode.com/posts/1'));
output.innerText = JSON.stringify(data, null, 2);
} catch (error) {
output.innerText = `Error: ${error.message}`;
}
});
</script>
</body>
</html>