Repository navigation
Expand file tree
/
Copy pathasync_function_composition.html
More file actions
56 lines (52 loc) · 1.9 KB
/
Copy pathasync_function_composition.html
File metadata and controls
56 lines (52 loc) · 1.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Async Function Composition in JavaScript</title>
<style>
button {
margin-top: 10px;
}
pre {
background-color: #f7f7f7;
padding: 10px;
}
</style>
</head>
<body>
<!--
In this example, we have a button labeled "Start Async Composition" that demonstrates async function composition. When the button is clicked, the main() function is invoked. The makeRequest() function makes an asynchronous request to an API endpoint using fetch() and awaits the response. Then, the processData() function simulates data processing by waiting for 2 seconds using setTimeout(). Finally, the processed data is displayed in the <pre> element.
-->
<h1>Async Function Composition in JavaScript</h1>
<p>Click the button below to demonstrate async function composition:</p>
<button id="startAsyncComposition">Start Async Composition</button>
<pre id="output"></pre>
<script>
const output = document.getElementById('output');
async function makeRequest(url) {
const response = await fetch(url);
const data = await response.json();
return data;
}
async function processData(data) {
// Simulating data processing
await new Promise(resolve => setTimeout(resolve, 2000));
const processedData = `Processed data: ${data.title}`;
return processedData;
}
async function main() {
try {
const data = await makeRequest('https://jsonplaceholder.typicode.com/posts/1');
const processedData = await processData(data);
output.innerText = processedData;
} catch (error) {
output.innerText = `Error: ${error.message}`;
}
}
document.getElementById('startAsyncComposition').addEventListener('click', () => {
main();
});
</script>
</body>
</html>