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
60 lines (56 loc) · 1.87 KB
/
app.js
File metadata and controls
60 lines (56 loc) · 1.87 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
"use strict";
/* Returns a promise designed to resolve with a list of hobbits, or potential fail.*/
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);
});
}
console.log(getList());
// TODO: Handle the resolved or rejected states of promise
let err = document.querySelector("#error");
let ul = document.querySelector("#list");
let promise=getList();
// TODO: If the promise resolves with the list of hobbits
// Render the list of hobbits as list items within the unordered list with id="list"
function handleList(list){
console.log(list);
list.forEach(hobbit => {
let li = document.createElement("li");
li.textContent = hobbit;
ul.appendChild(li);
});
}
// TODO: If promise rejects
// Display error message in the paragraph element with id="error"
/*Replace the console.log statement in the then method callback parameter to
iterate through the resolved list of hobbits
create a li for each hobbit, and append the li to the selected ul from the DOM
Replace the console.log statement in the catch method callback parameter to
display the resolved failure object's message property as the text content of the selected p from the DOM*/
function handleError(err){
console.log(err);
err.textContent=err.message;
err.textContent=err.message;
}
async function updateDOMList() {
try {
let list = await getList();
let errorMsg = error;
list.forEach((hobbit) => {
let li = document.createElement("li");
li.textContent = hobbit;
ul.appendChild(li);
});
} catch (err) {
console.error(err);
err.textContent = err.message;
}
}
updateDOMList();