Skip to content

Instantly share code, notes, and snippets.

@Lee182
Created November 17, 2015 20:52
Show Gist options
  • Save Lee182/2df28635297e9e7141ac to your computer and use it in GitHub Desktop.
Save Lee182/2df28635297e9e7141ac to your computer and use it in GitHub Desktop.
An simple xhr request for http getting stuff wraped in an promise so thenable
var get = function get(url) {
return promise = new Promise(function(done, reject) {
var req = new XMLHttpRequest()
req.onload = function() {
done(req.response)
};
req.onerror = function(e) {
reject(e)
}
req.open('get', url, true);
req.setRequestHeader('Accept', 'application/json');
req.send();
setTimeout(function(){
reject(new Error('10 seconds is way too long'))
},10000)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment