JavaScript Asynchronous Programming with JavaScript Understanding Promises From Callbacks to Promises

Mikel Cati
Mikel Cati
8,044 Points

.onerror error

Hello,

Is anyone else getting : "TypeError: xhr.onerror is not a function" in the console?

1 Answer

Mikel Cati
Mikel Cati
8,044 Points

```javascript function getJSON(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open("GET", url); xhr.onload = () => { if (xhr.status === 200) { let data = JSON.parse(xhr.responseText);

    resolve(data);
  } else {
    reject(Error(xhr.statusText));
  }
};
xhr.onerror = () => reject(Error("A network error occured"));
xhr.send();

}); }

also i`m getting an Error at XMLHttpRequest.xhr.onload

what am i doing wrong?