Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Manage Multiple Requests with Promise.all

Farid Lavizadeh
Farid Lavizadeh
12,006 Points

First image generated is wrong and is not the same as the first option list.

I noticed after using Promise.all the first image is not of the same breed anymore. I believe the same thing happened in the video. Why isn't the image generated the same as the first value of the list?

3 Answers

We can get rid of the generateImage function entirely, as well as the request for the first random image, if we make a small change in the fetchBreedImage function, and call it instead of the (now deleted) generateImage function. Giving the breed variable a default value of 'affenpinscher' ensures that the picture matches right away and gives a good intuitive sense of how the page works to the user. The changes to the fetchBreedImage function might look something like this:

function fetchBreedImage() {
  const breed = select.value || 'affenpinscher';
  card.innerHTML = '<img><p></p>'
  const img = card.querySelector('img');
  const p = card.querySelector('p');

  fetchData(`https://dog.ceo/api/breed/${breed}/images/random`)
    .then(data => {
      img.src = data.message;
      img.alt = breed;
      p.textContent = `Click to view more ${breed}s`;
    })
}

And the changes to the initial fetchData call look like this:

fetchData('https://dog.ceo/api/breeds/list/all')
.then( data => generateOptions(data.message))
.then(fetchBreedImage())

Worked great! Thanks!

Steven Parker
Steven Parker
231,248 Points

Before you select a breed, the code deliberately selects a random image.

Blaize Pennington
Blaize Pennington
13,879 Points

This bothered me as well. So I just called the fetchBreedImage function after the options were generated.

Danny L.
Danny L.
10,837 Points

Hi Blaize, this is bothering me as well. I've tried so many different things and tried calling the fetchBreedImage function just as you said but nothing seems to work. How did you do it?

All I could figure out to get it to work was to hard code it like this:

Promise.all([
  fetchData("https://dog.ceo/api/breeds/list/all"),
  fetchData("https://dog.ceo/api/breed/affenpinscher/images/random"),
  // fetchData("https://dog.ceo/api/breeds/image/random")
])
.then(data => {
  console.log(data)
  const breedList = data[0].message;
  // const randomImage = data[2].message;


  generateOptions(breedList);
  // generateImage(randomImage);
  const firstBreed = select[0].value;
  const firstImage = data[1].message;
  console.log(firstBreed);
  console.log(firstImage);
  generateImage(firstImage);
});