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, andgetCountry()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
awaitonfetch()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 itsawait. - 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, andPromise.all()then gathers the four pairs. TheseallSettled()calls fulfill even when a request fails, soPromise.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. Theawaitends when the last one lands, so the whole wait is about as long as the slowest request, not all eight added up. - Mateo typed
XXinstead ofMX, and his two requests failed in two different ways. Nager.Date answered404, so theokcheck ingetHolidays()threw itsError. - The World Bank answered
200, with a body that holds only a message saying the value isn’t valid. A200passes theokcheck, but that array holds only one item, at index0, sodata[1]isundefined. Readingdata[1][0]then throws aTypeError, andgetCountry()rejects. ThatTypeErrordoesn’t come fromfetch(), 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.