Last active
June 30, 2020 21:27
-
-
Save sreekotay/3a8a3c240ad2b76a67165c666a5720ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fetchXHR (url, httpMethod, body) { // httpMethod is optional | |
var xhr = new XMLHttpRequest(); | |
var promise = new Promise(function (resolve, reject) { | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState !== 4) return | |
if (xhr.status >= 200 && xhr.status < 300) resolve(xhr.response) | |
else reject({status: xhr.status, statusText: xhr.statusText, xhr:xhr}) | |
} | |
xhr.open(httpMethod || 'GET', url, true) | |
xhr.send(body) | |
}) | |
promise.abort = function() { xhr.abort() } | |
promise.xhr = xhr | |
return promise | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment