{"size": 816, "ext": "js", "max_stars_repo_path": "cfgov/unprocessed/apps/paying-for-college/js/util/promise-request.js", "max_stars_repo_name": "flacoman91/consumerfinance.gov", "max_stars_repo_stars_event_min_datetime": "2015-01-16T15:16:46.000Z", "max_stars_repo_stars_event_max_datetime": "2020-08-04T04:48:01.000Z", "max_issues_repo_path": "cfgov/unprocessed/apps/paying-for-college/js/util/promise-request.js", "max_issues_repo_name": "flacoman91/consumerfinance.gov", "max_issues_repo_issues_event_min_datetime": "2015-01-05T22:09:12.000Z", "max_issues_repo_issues_event_max_datetime": "2020-08-14T17:09:19.000Z", "max_forks_repo_path": "cfgov/unprocessed/apps/paying-for-college/js/util/promise-request.js", "max_forks_repo_name": "raft-tech/cfgov-refresh", "max_forks_repo_forks_event_min_datetime": "2015-01-28T14:51:18.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-10T00:00:39.000Z"}
{"max_stars_count": 156, "max_issues_count": 3604, "max_forks_count": 102, "avg_line_length": 23.3142857143, "max_line_length": 66, "alphanum_fraction": 0.6017156863}
/* Promise requests are better. */

/**
 * promiseRequest - A handy function for returning XHR Promises
 * @param {String} method - The method, ex. POST or GET
 * @param {String} url - The url to be requested
 * @returns {Object} Promise of the XHR request
 */
const promiseRequest = function( method, url ) {
  const xhr = new XMLHttpRequest();

  return new Promise( function( resolve, reject ) {

    // Completed xhr
    xhr.onreadystatechange = function() {
      // Do not run unless xhr is complete
      if ( xhr.readyState !== 4 ) return;
      if ( xhr.status >= 200 && xhr.status < 300 ) {
        resolve( xhr );
      } else {
        reject( new Error( xhr.status + ', ' + xhr.statusText ) );
      }
    };

    xhr.open( method, url, true );

    xhr.send();

  } );
};

export {
  promiseRequest
};
