Skip to content

Instantly share code, notes, and snippets.

@hcortezr
Last active March 9, 2018 03:22
Show Gist options
  • Save hcortezr/6318d1dcb08c9c371b3e7dbc4643a2a8 to your computer and use it in GitHub Desktop.
Save hcortezr/6318d1dcb08c9c371b3e7dbc4643a2a8 to your computer and use it in GitHub Desktop.
Promisifyed Web Request
HTTPRequest(
"GET",
"https://edroid.tk/projects/serverstatus/info.php",
null,
{responseType: "json"}
).then(({data}) => console.log(data));
/**
* HTTPRequest
* easy way to make http requests and with promises
*
* @author eDroid
*/
function HTTPRequest(method, url, data = null, request = {}){
if(!method && !url){
throw new Error("Must provide method and url!");
}
return new Promise((resolve, reject) => {
let req = Object.assign(new XMLHttpRequest(), request);
req.onreadystatechange = () => {
if(req.readyState === 4) resolve({
request: req,
data: req.response,
statusCode: req.status
});
};
req.onerror = e => reject(e);
req.open(method, url, true);
req.send(data);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment