Skip to curriculum
Jump to the editor

12.5. Run three tasks in sequence: chaining, then async/await

Easy
Topics
10–20 min

An order page loads in three steps that depend on each other: find the user, then that user's orders, then the total of those orders. Each step returns a promise, and each needs the result of the one before it. Writing that sequence well is the core skill of promise code, and it can be written two ways: as a chain of .then calls, or as an async function with await. Writing both shows what await really does.

In load.js, write loadWithThen(api, id) with a .then chain and loadWithAwait(api, id) with async/await. Both do the same work.

Signature

JAVASCRIPT
function loadWithThen(api, id)         // returns a Promise
async function loadWithAwait(api, id)  // returns a Promise

api has three methods, each returning a promise:

  • api.getUser(id) fulfills with a user object.
  • api.getOrders(user) fulfills with an array of that user's orders.
  • api.getTotal(orders) fulfills with a number.

Both functions fulfill with { user, orders, total }.

Requirements

  1. Each function returns a promise that fulfills with an object holding the user, the orders and the total.
  2. The steps run in order, getUser, then getOrders, then getTotal, each called with the previous step's result.
  3. Each step starts only after the previous one has fulfilled, and right away when it does.
  4. The returned promise settles only after the last step.
  5. If a step rejects, the returned promise rejects with that same error, and no later step is called.
  6. loadWithThen uses .then and contains no async or await. loadWithAwait is an async function that uses await and no .then.

Examples

JAVASCRIPT
const api = {
  getUser: (id) => Promise.resolve({ id, name: "Ada" }),
  getOrders: (user) => Promise.resolve([{ price: 30 }, { price: 12 }]),
  getTotal: (orders) => Promise.resolve(orders.reduce((sum, order) => sum + order.price, 0)),
};

await loadWithThen(api, "u1");
// { user: { id: "u1", name: "Ada" }, orders: [{ price: 30 }, { price: 12 }], total: 42 }
JAVASCRIPT
await loadWithAwait(api, "u1"); // the same object
JAVASCRIPT
const broken = { ...api, getOrders: () => Promise.reject(new Error("orders are down")) };

await loadWithAwait(broken, "u1"); // rejects with "orders are down"; getTotal is never called

Environment

  • Plain JavaScript in a browser page; the tests call the globals loadWithThen and loadWithAwait from load.js.
  • The tests' api answers on a virtual clock (setTimeout), so no test waits for real time.
  • The api methods always return promises; a method that throws instead is out of scope.
  • Do not catch the errors: the caller handles them.

Hints

  1. A value returned from a .then handler becomes the next promise's value. How does the last handler still see the user from the first step?
  2. What does await do when the promise it waits for rejects?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…