Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Watch Four Countries Load Together

In this lesson, you started all eight of the planner’s requests at once, a holidays request and a country request for each of the four friends. You watched the answers land in their own order and gave each friend their own results, including Mateo’s two failures.

Key Points

  • Four friends make eight requests, because getHolidays() asks Nager.Date for a friend’s holidays, and getCountry() asks the World Bank for their country’s name and capital city. Asked one at a time, each request waits for the one before it to finish. None of them needs another one’s answer, so all eight can run together. The deadline from your exercise is left out here, to keep the code short.
  • Each call runs its helper up to the await on fetch() and hands back a promise that stays pending while the request is in progress. So when the .map() callback has called both functions for every friend, all eight requests are lined up, two per friend, and they go out the moment the planner pauses at its await.
  • The World Bank could take all four codes in one request, but with one request per friend, a mistake costs only that friend.
  • Promise.allSettled() waits until every promise has settled, and it doesn’t reject when one of them rejects. It fulfills with one object per promise: { status: "fulfilled", value } or { status: "rejected", reason }.
  • One allSettled() per friend keeps that friend’s two results together, and Promise.all() then gathers the four pairs. These allSettled() calls fulfill even when a request fails, so Promise.all() can’t reject either. Its results keep the friends’ order.
  • The answers can land in a different order on every run, and each result goes to its own friend, wherever it lands in time. The loop reads each friend’s pair back by the same position, with const [holidays, country] = results[i];, holidays first, then country. The await ends when the last one lands, so the whole wait is about as long as the slowest request, not all eight added up.
  • Mateo typed XX instead of MX, and his two requests failed in two different ways. Nager.Date answered 404, so the ok check in getHolidays() threw its Error.
  • The World Bank answered 200, with a body that holds only a message saying the value isn’t valid. A 200 passes the ok check, but that array holds only one item, at index 0, so data[1] is undefined. Reading data[1][0] then throws a TypeError, and getCountry() rejects. That TypeError doesn’t come from fetch(), because the request worked. It comes from your own line, which expects a shape this body doesn’t have.
  • Mateo’s failures stayed in Mateo’s results. The other three friends kept every answer.

The rule is to start every request first, wrap each friend’s requests in Promise.allSettled(), await them all with Promise.all(), then read each result back by its position. That way, one friend’s failure never hides another friend’s answer.

The Final Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The planner can now ask Nager.Date for a friend’s holidays, and the World Bank for their country’s name and capital city. The deadline from your exercise is left out here, to keep the code short.

00:11 Four friends make eight requests. Asked one at a time, each request waits for the one before it to finish. But none of them needs another one’s answer, so all eight can run together.

00:25 In this video, you’ll start all eight at once, watch them land, and give each friend their own result, even when a request fails.

00:34 Here are the four friends, each with a country code. Amara’s in Portugal, Jonas is in Germany, and Keiko’s in Japan. Mateo meant to type MX, but he typed XX instead.

00:48 Leave it that way for now, to see what a mistake does to everyone else. The map callback calls both functions for each friend. Each call runs its helper up to the await on fetch, and hands back a promise that stays pending while the request is in progress.

01:05 So when map finishes, all eight requests are lined up, two per friend, and they go out the moment the planner awaits. The World Bank could take all four codes in one request, but with one request per friend, a mistake costs only that friend.

01:23 Each friend’s two promises go into Promise.allSettled(), which you’ve met before. It waits until every promise has settled, and it doesn’t reject when one of them rejects.

01:34 It fulfills with one object per promise: a status of fulfilled with a value, or rejected with a reason. One allSettled per friend keeps that friend’s two results together.

01:46 Promise.all() then gathers the four pairs. These allSettled calls fulfill even when a request fails, so Promise.all can’t reject either. Its results keep the friends’ order.

02:00 Now watch the eight answers come back. They can land in a different order on every run. Each result drops into its own slot, in its own friend’s row, wherever it lands in time.

02:13 The await ends when the last one lands, so the whole wait is about as long as the slowest request, not all eight added up.

02:22 The loop reads each friend’s pair back by the same position, holidays first, then country. Run the script. Amara, Jonas and Keiko each get a country, a capital city, and a count of holidays.

02:36 Mateo’s line says failed, with two different reasons.

02:41 The first is the Error that getHolidays throws from its ok check. Nager.Date answered 404, so the check threw.

02:51 The second is stranger. The World Bank answered 200, with a body that holds only a message saying the value isn’t valid. Here’s the last line of getCountry, which reads the data at index one, then at index zero.

03:06 Your turn: pause the video and predict what the data at index one is for this body. It’s undefined. A 200 passes the ok check, but this array holds only one item, at index zero, so there’s nothing at index one.

03:23 Reading index zero of undefined throws a TypeError, so getCountry rejects. This TypeError doesn’t come from fetch, because the request worked. It comes from your own line, which expects a shape this body doesn’t have.

03:39 Either way, Mateo’s failures stayed in Mateo’s row. The other three friends kept every answer.

03:47 Here’s the rule: start every request first, wrap each friend’s requests in allSettled, await them all with Promise.all, then read each result back by its position.

04:00 That way, one friend’s failure never hides another friend’s answer.

04:05 A 200 isn’t always the data you asked for, though. Next, in Check Every Response’s Shape, you’ll make the planner check each body before it reads it, and say clearly when a country has no data.

Become a Member to join the conversation.