Last active
September 2, 2017 01:22
-
-
Save patmigliaccio/347ff01685132328ad63aeba840efaac to your computer and use it in GitHub Desktop.
patmigliaccio.com/async-fishing 12/11/16
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
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