Skip to content

Commit 97ebd9b

Browse files
committed
Fetch with Function
1 parent 99e18ba commit 97ebd9b

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

app.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@
44

55
const url = "./api/people.json";
66

7-
/* Fetch is a builtin functionality that makes to do less work for setup so we can start implementating */
8-
fetch(url)
9-
.then((response) => console.log(response))
10-
.then((data) => console.log(data))
11-
.catch((error) => console.log(error));
7+
const btn = document.querySelector(".btn");
128

13-
const promise = new Promise((resolve, reject) => {
14-
let value = false;
15-
if (value) {
16-
resolve([1, 2, 5]);
17-
} else {
18-
reject(new Error(`The value is ${value}`));
19-
}
20-
return promise;
9+
btn.addEventListener("click", () => {
10+
fetch(url)
11+
.then((response) => response.json())
12+
.then((data) => {
13+
// console.log(data);
14+
displayPeople(data);
15+
})
16+
.catch((error) => console.log(error));
2117
});
2218

23-
promise.then((data) => console.log(data)).catch((error) => console.log(error));
19+
const displayPeople = (people) => {
20+
const displayData = people
21+
.map((person) => {
22+
const { name } = person;
23+
return `<p>${name}</p>`;
24+
})
25+
.join("");
26+
const element = document.createElement("div");
27+
element.innerHTML = displayData;
28+
document.body.appendChild(element);
29+
};

0 commit comments

Comments
 (0)