-
-
Save robert-carroll/e5afcd954c2330bb170d7cd1cb6b0ace to your computer and use it in GitHub Desktop.
even simpler async/await ajax
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 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