// defining XHR function
//
const XHR = (url = 'http://localhost:3000/data/search.json', callback = undefined) => {
  return new Promise((resolve, reject) => {
    let request = new XMLHttpRequest();
    request.open('GET', url, true);
    request.send();
    request.onreadystatechange = () => {
      if (request.readyState === 4) {
        if (request.status === 200) {
          let parsed = JSON.parse(request.response);
          resolve(parsed);
        } else {
          reject(`XHR rejected ${request.status}: ${request.statusText}`);
        }
      } else {
        reject(`XHR rejected ${request.status}: ${request.statusText}`);
      }
    };
  });
};

// exporting function
module.exports = {
  XHR
};

// Usage
// XHR()
//   .then(result => console.log(result)
//   .then(result2 => console.log(result2)
//   .catch(error => console.log(error);

@@ -8,16 +8,25 @@
 ning XHR
+(request)
  functio
@@ -31,11 +31,8 @@
 ion%0A
-//%0A
 cons
@@ -43,78 +43,11 @@
 R = 
-(
 url
- = 'http://localhost:3000/data/search.json', callback = undefined)
  =%3E 
@@ -200,18 +200,34 @@
 request.
-on
+addEventListener('
 readysta
@@ -234,18 +234,18 @@
 techange
- =
+',
  () =%3E %7B
@@ -267,24 +267,22 @@
 est.
-readyState
+status
  === 
-4
+200
 ) %7B%0A
@@ -297,38 +297,40 @@
 if (request.
-status
+readyState
  === 
-200
+4
 ) %7B%0A        
@@ -335,21 +335,16 @@
     
-let parsed = 
+resolve(
 JSON
@@ -367,16 +367,17 @@
 esponse)
+)
 ;%0A      
@@ -382,35 +382,16 @@
     
-  resolve(parsed);%0A 
+%7D%0A
       
- 
 %7D el
@@ -391,26 +391,24 @@
    %7D else %7B%0A
-  
         reje
@@ -416,117 +416,42 @@
 t(%60X
-HR rejected $%7Brequest.status%7D: $%7Brequest.statusText%7D%60);%0A        %7D%0A      %7D else %7B%0A        reject(%60XHR rejected
+MLHttpRequest rejected with status
  $%7Br
@@ -504,16 +504,24 @@
  %7D%0A    %7D
+, false)
 ;%0A  %7D);%0A
@@ -595,49 +595,48 @@
 XHR(
-)%0A//   .then(result =%3E console.log(result
+'http://localhost:3000/data/search.json'
 )%0A//
@@ -650,17 +650,16 @@
 n(result
-2
  =%3E cons
@@ -670,15 +670,40 @@
 log(
-result2
+JSON.stringify(result, null, 2))
 )%0A//
@@ -725,24 +725,23 @@
  =%3E 
-console.log
+new Error
 (error)
+)
 ;%0A

update xhr request