Skip to content

Instantly share code, notes, and snippets.

@robert-carroll
Last active October 24, 2018 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robert-carroll/e5afcd954c2330bb170d7cd1cb6b0ace to your computer and use it in GitHub Desktop.
Save robert-carroll/e5afcd954c2330bb170d7cd1cb6b0ace to your computer and use it in GitHub Desktop.
even simpler async/await ajax
var pbj = {};
pbj.xhr = url => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://reqres.in' + url);
xhr.onload = () => resolve(xhr.response);
xhr.onerror = () => reject(xhr.status);
xhr.send();
})
};
pbj.load = async function (url) {
return await pbj.xhr(url);
};
pbj.load('/api/users?delay=3').then(response => {
console.log(response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment