Last active
March 9, 2018 03:22
-
-
Save hcortezr/6318d1dcb08c9c371b3e7dbc4643a2a8 to your computer and use it in GitHub Desktop.
Promisifyed Web Request
This file contains 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
HTTPRequest( | |
"GET", | |
"https://edroid.tk/projects/serverstatus/info.php", | |
null, | |
{responseType: "json"} | |
).then(({data}) => console.log(data)); |
This file contains 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
/** | |
* 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