Native XHR request as Promise

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

fetch("https://api.coinmarketcap.com/v1/ticker/?limit=10")
  .then(jsonRes => {
    console.log(jsonRes);
  })
  .catch(e => {
		console.error(e);
  });

function fetch(url) {
  // Create new promise with the Promise() constructor;
  // This has as its argument a function
  // with two parameters, resolve and reject
  return new Promise(function(resolve, reject) {
    // Standard XHR to load an image
    var request = new XMLHttpRequest();
    request.open('GET', url);    
    // When the request loads, check whether it was successful
    request.onload = function() {
      if (request.status === 200) {
        // If successful, resolve the promise by passing back the request response
        resolve(request.response);
      } else {
        // If it fails, reject the promise with a error message
        reject(Error(`XHR request for ${url} failed. Error code: ${request.statusText}`));
      }
    };
    request.onerror = function() {
      // Also deal with the case when the entire request fails to begin with
      // This is probably a network error, so reject the promise with an appropriate message
      reject(Error('There was a network error.'));
    };
    // Send the request
    request.send();
  });
}