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
34 lines (32 loc) · 1.25 KB
/
app.js
File metadata and controls
34 lines (32 loc) · 1.25 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
/*
*
* @returns A promise that is designed to resolve with a list of hobbits, or potentially fail with an failure object. The failure object includes a boolean success property and a string message property.
*/
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 p = document.getElementById("error");
let ul = document.getElementById("list")
// TODO: Handle the resolved or rejected states of the promise
getList().then((hobbits) => {
hobbits.forEach(hobbit);
let li = document.createElement("li");
li.textContent = hobbit
ul.appendChild(li)
})
.catch((err)=> {
p.textContent = err.message;
});
// 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" (check the index.html file)
// TODO: If the promise rejects with the failure object
// Display the failure message in the paragraph element with id="error" (check index.html file)