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

Follow Every Page of Results

In this lesson, the planner looked up Mateo’s country code by its name, Mexico, in the World Bank’s list of countries, a list that comes back one page at a time. You followed it from page to page until it ran out, with a limit on how many pages the loop may ask for.

Key Points

  • You know Mateo meant MX, but the planner can’t guess that from XX. Every friend knows their country’s name, though, and Mateo’s is Mexico. The World Bank lists every country with its code, so the planner can look the code up by name.
  • A first try asks for the whole list, https://api.worldbank.org/v2/country?format=json. The first element of the body holds the paging fields. They say this is page one of six, and the list has 296 entries in all. The second element holds only 50 of them. Mexico isn’t among those 50, so .find() gives undefined.
  • Watch out: a list endpoint can answer with its first page only. Nothing fails, and only the paging fields say the rest is missing. The 296 also include regions and income groups, such as Africa Eastern and Southern.
  • getAllCountries() asks again, once per page. Its loop counts pages from one, as the World Bank does, and never past maxPages, ten by default. Each pass builds the URL with that page number and a page size of 50. Many APIs cap that size, so bigger pages can’t replace the loop. It leaves out the last lesson’s shape check, to stay short.
  • Each pass checks response.ok, splits the body into the paging info and the entries, and adds the entries to the end of one list.
  • The stop condition reads the paging fields: when the page that came back reaches the number of pages, nothing’s left to ask for, so the function returns the list.
  • With six pages, the loop sends six requests, page one included, because it starts over at page one. It returns at page six, long before ten. Mexico comes by on page four, but the loop keeps going, because the script looks for Mexico only once it has the whole list. Then .find() looks up the name Mexico. It found an entry, so the check passes, and MX is the code it logs. The planner found MX from the name alone.
  • The loop’s second way to stop is maxPages, a limit you choose, ten unless you pass a number. Without a limit, a server that never said it was on the last page would keep the loop asking forever. When the passes run out before the last page, the function’s promise rejects, with an error that says why, and capped.mjs catches it. It doesn’t return part of the list, which could miss Mexico without any error, like the first try.

The rule to remember is that a list can come back one page at a time. Read its paging fields, and ask for each next page until the page number, page, reaches the number of pages, pages. Set a limit too, one that says so when it’s hit.

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 You know Mateo meant MX, but the planner can’t guess that from XX. Every friend knows their country’s name, though, and Mateo’s is Mexico. The World Bank lists every country with its code, so the planner can look it up by name.

00:15 In this video, you’ll follow that list from page to page until it runs out.

00:20 The first try is a script that asks for the whole list and logs three things. Run the script. The first element holds the paging fields. They say this is page one of six, and the list has 296 entries in all.

00:35 The second element holds only fifty of them.

00:39 Mexico isn’t among those fifty, so the third line logs undefined. Watch out: a list endpoint can answer with its first page only. Nothing fails, and only the paging fields say the rest is missing.

00:55 And the 296 include regions and income groups, such as Africa Eastern and Southern.

01:03 So the planner asks again, once per page. The new module exports getAllCountries, which returns the whole list. It leaves out the last lesson’s shape check, to stay short.

01:15 Its loop counts pages from one, as the World Bank does, and never past maxPages, ten here. Each pass builds the URL with that page number and a page size of fifty.

01:28 Many APIs cap that size, so bigger pages can’t replace the loop. It checks ok and splits the body into the paging info and the entries. The entries go onto the end of one list.

01:42 Then comes the stop condition. When the page that came back reaches the number of pages, nothing’s left to ask for, so the function returns the list.

01:52 The first answer said there are six pages, and the loop starts over at page one. Your turn: pause the video and predict how many requests this loop sends before it returns.

02:05 It sends six, page one included. The loop returns at page six, long before ten. This script calls it, then looks up Mexico in the list. Run it. On the first pass, page one of six arrives, and fifty entries join the list.

02:23 The next pass asks for page two, then three. Mexico comes by on page four, but the loop keeps going, because the script looks for Mexico only once it has the whole list.

02:35 Page six of six arrives with the last forty-six entries. There are no pages left, so the loop stops. The script logs 296 entries, all of them this time. Then .find() looks up the name Mexico.

02:51 It found an entry, so the check passes, and MX is the code it logs. The planner found MX from the name alone. If the World Bank doesn’t answer, the text below this video has the saved pages and a module that serves them.

03:08 The loop’s second way to stop is maxPages, a limit you choose, ten unless you pass a number. Six pages fit under it. Without a limit, a server that never said it was on the last page would keep the loop asking forever.

03:24 When the passes run out before the last page, the function’s promise rejects, with an error that says why. It doesn’t return part of the list, which could quietly miss Mexico, like the first try.

03:37 Here’s a run with a limit of three. Three pages arrive, then the catch logs the message: stopped after three pages, before the last page.

03:48 The rule is that a list can come back one page at a time. Read its paging fields, and ask for each next page until the page number reaches the number of pages.

03:59 Set a limit too, one that says so when it’s hit.

04:03 Some requests fail for a while, then work again. Next, in Retry Only What Might Succeed, you’ll decide which failed requests deserve a second try.

Become a Member to join the conversation.