CodeBlanks Logo Icon CodeBlanks

Summary - Promises

JavaScript course · Intermediate · Lesson 77/134

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

  1. Match the chain methods

    then
    Catches success
    catch
    Catches error
    finally
    Does cleanup
    Promise.all
    Waits in parallel
  2. 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());

All lessons in Intermediate