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

Colton Shaw
Colton Shaw
11,929 Points

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();
});