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

Promise is returned with incomplete data

Hi, I just came across this problem when working on the "How many people are in space" video and thought it might help to share the workaround I came up with.

The Promise from getProfiles is being correctly returned, however one of the astronauts has a common name at this current time. Wikipedia returns a "disambiguation" page instead of a "standard" page. This means that in generateHTML when the template literal is being constructed for the <img> element, person.thumbnail is undefined.

Edit: to clarify, this means that the JSON is properly returned, but execution is halted during HTML generation and the elements are not displayed.

As a workaround, I created a new function to check for a disambiguous entry to handle this particular case and add in a thumbnail.source property to allow execution to continue. Finally, I used .then() after getting the JSON for each person and passed the person object to the new disambiguous checker:

function isDisambiguous(entry) {
  const disambImg = "https://i.kym-cdn.com/photos/images/original/001/285/460/8b6.jpg"
  if (entry.type === "disambiguation") {     // handle the disambiguation case
    entry.thumbnail = {
      source: disambImg                      // add missing image url
    };
  } return entry                             // return person object, changed or unchanged
}

function getProfiles(json) {
  const profiles = json.people.map(person => {
    return getJSON(wikiUrl + person.name)    // get our regular data
      .then(isDisambiguous);                 // check for disambiguation
  });
  return Promise.all(profiles);
};```

1 Answer

Yeah, I had that same problem. You can either ignore it, or make a workaround to catch that issue.

There are a couple of astronauts with ambiguous names.