Exercise: JS Promises
Objective#
Your objective is to handle the result of a promise with promise consumers, and update the DOM based on resolution or rejection. The Promise will resolve with an array of hobbits that should be rendered on the DOM in the form of list items. If any errors occur, feedback should be displayed to the user on the DOM.
Getting Started#
Fork and Close the Exercise Repo to get started: JavaScript Promises
Steps#
The JavaScript Promises exercise has multiple steps:
Exercise 1: Select the Needed DOM Elements#
- Select the paragraph with
id="error"and assign it to a variable - Select the unordered list with
id="list"and assign it to a variable
Exercise 2: Handle the Promise#
- Call
getList - From the result, call the
thenpromise consumer method and pass in a callback function- The callback function should receive the resolved value as the parameter
- Start out just logging the result to console to check if it is the value you expect
- From the result of the
thenpromise consumer method, chain acatchmethod consumer and pass in a callback function- The callback function should receive the resolved value as the parameter
- Start out just logging the result to console to check if it is the value you expect
Exercise 3: Update the DOM#
- Replace the
console.logstatement in thethenmethod callback parameter to- iterate through the resolved list of hobbits
- create a
lifor each hobbit, and append thelito the selectedulfrom the DOM
- Replace the
console.logstatement in thecatchmethod callback parameter to- display the resolved failure object's
messageproperty as the text content of the selectedpfrom the DOM
- display the resolved failure object's
Helpful Links#
If you feel stuck, or would like to see the finished code for this exercise to check your work, check out: