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/efacd880e75267ec444febc7859d7039 to your computer and use it in GitHub Desktop.
Save robert-carroll/efacd880e75267ec444febc7859d7039 to your computer and use it in GitHub Desktop.
a little async/await promise test
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 (config) {
console.log(config)
var cfg = Object.values(config),
cfg = cfg.sort(() => Math.random() - 0.5),
url = cfg[0];
console.log(url)
var a = await pbj.xhr(url);
return {
a: a,
b: 'b',
c: {}
}
}
pbj.config = {
opt0: '/api/users/2',
opt1: '/api/users?delay=3',
opt2: '/api/unknown/404'
};
pbj.load(pbj.config).then(payload => {
console.log(payload)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment