JeremyE
JeremyE

Reputation: 1418

Infinite loop when making Axios GET Request - Loop Won't Close

I'm a pretty big newb when it comes to working with API's and promises - so I'm having a hard time wrapping my head around this issue.

The goal is to make a GET request to an API and have those results returned. However, since the API spreads data across multiple pages, I'm going to loop through the API pages until I get back 0 listings.

I'm able to successfully log listings to the console, however, the while loop inside of loopThroughListings never seems to close despite listingsExist being set to false.

Where did I go wrong?

const axios = require('axios');

// Loop through multiple pages of listings in the API
function loopThroughListings() {
  let listingsExist = true;

  // Loop through listings until they don't exist
  while(listingsExist) {

    getListing().then(function(listing) {

      if(listing.length < 1 ) {
        // if listings don't exist, stop the loop
        // THIS IS WHERE THE ISSUE IS
        console.log("No Listings");
        listingsExist = false;
      } else {
        // if listings do exist, log them to console
        console.log(listing);
      }

    });

  }
}

// Return listing data from API
async function getListing(page) {
  try {
    const response = await axios.get(`https://mlb19.theshownation.com/apis/listings.json?type=Stadium&page=1}`);
    return response.data.listings;
  } catch (error) {
    console.error(error);
  }
}

loopThroughListings();

Upvotes: 1

Views: 4367

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

You'll need loopThroughListings to be async and then you can await getListing()

Like so:

const axios = require('axios');

// Loop through multiple pages of listings in the API
async function loopThroughListings() {
    let listingsExist = true;

    // Loop through listings until they don't exist
    while (listingsExist) {
        const listing = await getListing();
        if (listing.length < 1) {
            // if listings don't exist, stop the loop
            // THIS IS WHERE THE ISSUE IS
            console.log("No Listings");
            listingsExist = false;
        } else {
            // if listings do exist, log them to console
            console.log(listing);
        }
    }
}

// Return listing data from API
async function getListing(page) {
    try {
        const response = await axios.get(`https://mlb19.theshownation.com/apis/listings.json?type=Stadium&page=1}`);
        return response.data.listings;
    } catch (error) {
        console.error(error);
    }
}

loopThroughListings();

Personally, since the while loop will of course run at least once, I'd use do/while instead - to me it makes the flow of the code more apparent

    do {
        const listing = await getListing();
        if (listing.length < 1) {
            // if listings don't exist, stop the loop
            // THIS IS WHERE THE ISSUE IS
            console.log("No Listings");
            listingsExist = false;
        } else {
            // if listings do exist, log them to console
            console.log(listing);
        }
    } while(listingsExist);

But that's just a matter of opinion

Upvotes: 2

Related Questions