Summary - Promises
Write a user-fetch-and-display flow using a promise chain.
Summary - Promises
In this group you learned the three promise states (pending, fulfilled, rejected), creating promises with new Promise, and chaining with then, catch and finally. You also saw Promise.all for parallel work.
Now you will put it all together in a flow: fetch and show a user's info using a promise chain.
Exercise User flow chain
A function called fetchUser returns a promise. Build this chain: call fetchUser; in then log the user's name property with console.log; in catch log the error with console.log; in finally call hideSpinner.
Show example solution
fetchUser()
.then(user => console.log(user.name))
.catch(error => console.log(error))
.finally(() => hideSpinner());
Test yourself
-
Match the chain methods
- then
- Catches success
- catch
- Catches error
- finally
- Does cleanup
- Promise.all
- Waits in parallel
-
Complete the full chain
fetchUser() .then(user => console.log(user)) .____(error => console.log(error)) .finally(() => hideSpinner());Show answer
fetchUser() .then(user => console.log(user)) .catch(error => console.log(error)) .finally(() => hideSpinner());