Last active
April 17, 2018 20:21
-
-
Save anabastos/ff87fd9ee8c46b09ad8125a0eb5f02d4 to your computer and use it in GitHub Desktop.
*~my Fetch~*
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
const fetch = (url, verb, onSuccess, onFail) => { | |
const asyncqued = !!onSuccess; | |
asyncqued && req.onreadystatechange = processReqChange; | |
const req = (window.XMLHttpRequest && new XMLHttpRequest()) | |
|| (window.ActiveXObject && new ActiveXObject("Microsoft.XMLHTTP")); | |
callback(req, onSuccess, onFail); | |
req.open(verb, url, asyncqued); | |
req.send(null); | |
return asyncqued ? req : req.responseText; | |
} | |
const callback = (req, onSuccess, onFail) => { | |
if (req.readyState == 4) { | |
if (req.status == 200) { | |
onSuccess && onSuccess(req.responseText, url, req); | |
} else { | |
onFail && onFail(url, req); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment