Created
November 17, 2015 20:52
-
-
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
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
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