0

I'm fetching from an API that returns paginated results with a link to the next page, and when I receive that response, I'd like to get the remaining pages. I am trying to loop through, but I think I have an issue with promises.

The API returns: {page: 1, pages: 79, per_page: 100, items: 7806, urls: {next: "https://api..com/artists//releases?page=2&per_page=100"}}

My solution so far:

const getPaginatedResults = (id) => {
    
    fetch(`/getArtistReleases`, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({id}),
    })
    .then((res) => res.json())
    .then((data) => {

      let nextPaginationUrl = data.data.pagination.urls.next
      let numPages = data.data.pagination.pages
      
      for (let i = 0; i < numPages; i++){
        
        fetch(paginationUrl)
        .then((res) => res.json())
        .then((data) => {
          paginationUrl = data.pagination.urls.next
        })
      }
    })
    .catch((err) => console.log(err));
  }

Which keeps returning only page 2, not 2,3,4,5,6,7 etc. I am guessing it's an issue with the promises, which I am a bit fuzzy on how they work, so if anyone can point out what I'm missing here, much appreciated!

Thank you!

DBN
  • 41
  • 3

0 Answers0