-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
90 lines (74 loc) · 2.07 KB
/
page.tsx
File metadata and controls
90 lines (74 loc) · 2.07 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"use client";
import { useEffect, useState } from "react";
const getListUsingThen = () => {
const url = "/.netlify/functions/chicagomusiccompass";
const results = fetch(url)
.then((response) => response.json())
.catch(() => []);
return results;
};
const getListUsingAwait = async () => {
const url = "/.netlify/functions/chicagomusiccompass";
try {
const response = await fetch(url);
const results = await response.json();
return results;
} catch (error) {
return [];
}
};
interface Event {
name: string;
}
export default function Page() {
const [list1, setList1] = useState<Event[]>([]);
const [list2, setList2] = useState<Event[]>([]);
useEffect(() => {
getListUsingThen().then((list) => setList1(list));
}, []);
useEffect(() => {
const fetch = async () => {
const list = await getListUsingAwait();
setList2(list);
};
fetch();
}, []);
return (
<main style={{ maxWidth: 800, margin: "0 auto", textAlign: "center" }}>
<h1 style={{ margin: "20px 0" }}>Try/Catch vs .then().catch()</h1>
<div style={{ marginTop: 40 }}>
<h2 style={{ margin: "12px 0", borderBottom: "1px dotted black" }}>
List using `then()`
</h2>
<ul style={{ listStyle: "none" }}>
{list1.map((item) => (
<li key={item.name}>{item.name}</li>
))}
</ul>
</div>
<div style={{ marginTop: 40 }}>
<h2 style={{ margin: "12px 0", borderBottom: "1px dotted black" }}>
List using `await()`
</h2>
<ul style={{ listStyle: "none" }}>
{list2.map((item) => (
<li key={item.name}>{item.name}</li>
))}
</ul>
</div>
<a
href="https://github.com/garciadiazjaime/demo-reactjs/blob/main/app/try-catch-vs-then-catch/page.tsx"
style={{
textDecoration: "underline",
fontSize: 24,
display: "block",
textAlign: "center",
marginTop: 20,
}}
rel="nofollow"
>
github repository
</a>
</main>
);
}