Repository navigation
Expand file tree
/
Copy pathasync_promise_decorator.html
More file actions
60 lines (54 loc) · 2.03 KB
/
Copy pathasync_promise_decorator.html
File metadata and controls
60 lines (54 loc) · 2.03 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Promise Decorator in JavaScript</title>
<style>
button {
margin-right: 10px;
}
</style>
</head>
<body>
<!--
In this example, we have a button that demonstrates a promise decorator. The "Fetch Data with Logger" button fetches data from an endpoint using the makeRequestWithLogger function, which logs when the request starts and completes. The fetched data is then displayed in a formatted way.
-->
<h1>Promise Decorator in JavaScript</h1>
<p>Click the button below to demonstrate a promise decorator:</p>
<button id="fetchWithLogger">Fetch Data with Logger</button>
<pre id="output"></pre>
<script>
const output = document.getElementById('output');
const apiUrl = 'https://jsonplaceholder.typicode.com/todos/1';
function printResult(result) {
output.innerText = JSON.stringify(result, null, 2);
}
function makeRequest(url) {
return new Promise((resolve, reject) => {
fetch(url)
.then(response => response.json())
.then(data => resolve(data))
.catch(error => reject(error));
});
}
function logger(fn) {
return function (...args) {
console.log('Starting function...');
return fn(...args).then(result => {
console.log('Function completed.');
return result;
});
};
}
const makeRequestWithLogger = logger(makeRequest);
document.getElementById('fetchWithLogger').addEventListener('click', () => {
makeRequestWithLogger(apiUrl)
.then(data => printResult(data))
.catch(error => {
output.innerText = `Error: ${error.message}`;
});
});
</script>
</body>
</html>