Skip to content

Instantly share code, notes, and snippets.

@patmigliaccio
Last active September 2, 2017 01:22
Show Gist options
  • Save patmigliaccio/347ff01685132328ad63aeba840efaac to your computer and use it in GitHub Desktop.
Save patmigliaccio/347ff01685132328ad63aeba840efaac to your computer and use it in GitHub Desktop.
patmigliaccio.com/async-fishing 12/11/16
function xhr({method = 'GET', url, async = true, responseType = ''}){
let req = new XMLHttpRequest();
req.open(method, url, async);
req.responseType = responseType;
let p = new Promise((resolve, reject) => {
req.onreadystatechange = () => {
if (req.readyState == XMLHttpRequest.DONE){
if (200 <= req.status && req.status < 300){
resolve(req.response);
} else {
reject(req.response);
}
}
}
req.send();
});
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment