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 Asynchronous Programming with JavaScript Understanding Promises From Callbacks to Promises

Uncaught Type Error and Uncaught reference error when making Promises

The snapshot url is http://w.trhou.se/vjb4r691g0

I get errors on line 44 and line 12. Uncaught type error and uncaughtReferenceError. If you could explain the error in terms of the process of how a Promise operates that'd be great! This is my first JS course so I'm drinking from a firehose!!

1 Answer

Jonathan Ambriz, You were very close it looks like you may have just not one Promise call and a >. See below!

function getJSON(url) {
  return new Promise ((resolve,reject) => { //this was not declared which means the entire promise structure created would not work.
    const xhr = new XMLHttpRequest();
    xhr.open('GET', url);
    xhr.onload = () => {
      if(xhr.status === 200) {
        let data = JSON.parse(xhr.responseText);
        console.log(data);
        resolve(data);
      }else {
        reject( Error(xhr.statusText) );
      }
    };
    xhr.onerror = () => reject( Error('A network error occured.') );
    xhr.send();
  });
}

btn.addEventListener('click', (event) => {
  getJSON(astrosUrl)
    .then(getProfiles)
    .then(data => console.log(data))
    .catch(err => console.log(err)) //You were missing a > right here which is why this failed.
  event.target.remove();
});