Skip to content

Instantly share code, notes, and snippets.

@monostere0
Last active August 29, 2015 14:13
Show Gist options
  • Save monostere0/613d852f2267ac888b48 to your computer and use it in GitHub Desktop.
Save monostere0/613d852f2267ac888b48 to your computer and use it in GitHub Desktop.
XHR using ES6 Promise
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