Last active
August 29, 2015 14:13
-
-
Save monostere0/613d852f2267ac888b48 to your computer and use it in GitHub Desktop.
XHR using ES6 Promise
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
var xhr = function(x, y, z) { | |
var a; | |
return new Promise(function(o, p) { | |
with(a = new XMLHttpRequest) open(y || "GET", x), send(z || null), onload = function(n) { | |
o(n.target.responseText) | |
}, onerror = p | |
}) | |
}; | |
//usage: | |
// GET - xhr('http://www.mysite.com/api/etc').then(function(data) { console.log(data) }); | |
// POST - xhr('http://www.mysite.com/api/etc', 'POST', {hello:1}).then(function(data){ console.log(data) }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment