forked from benrbryant/JavaScript_Promises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
31 lines (26 loc) · 780 Bytes
/
app.js
File metadata and controls
31 lines (26 loc) · 780 Bytes
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
async function getList() {
return new Promise((resolve, reject) => {
setTimeout(() => {
let potentialFail = Math.round(Math.random() * 100) < 10;
if (potentialFail) {
reject({ success: false, message: "Failed to get list of hobbits." });
} else {
resolve(["Bilbo", "Frodo", "Sam", "Merry", "Pippin"]);
}
}, 10);
});
}
let feedbackP = document.querySelector("#error");
let ul = document.querySelector("#list");
async function updateDOMList() {
try {
let list = await getList();
list.forEach((hobbit) => {
let li = document.createElement("li");
li.textContent = hobbit;
ul.appendChild(li);
});
} catch (err) { console.error(err);
feedbackP.textContent = err.message;}
}
updateDOMList();