JSFiddle - React, Tailwind, and code Playground
by Pranab Dey
JavaScript
class HttpError extends Error {
constructor(response) {
super(`${response.status} for ${response.url}`);
this.name = 'HttpError';
this.response = response;
}
}
async function loadJson(url) {
var f = await fetch(url);
if (f.status == 200) {
return f.json();
} else {
throw new HttpError(f);
}
}
// Ask for a user name until github returns a valid user
async function demoGithubUser() {
let name = prompt("Enter a name?", "iliakan");
let res = await loadJson(`https://api.github.com/users/${name}`);
alert(`Full name: ${res.name}.`);
return res;
.catch(err => {
if (err instanceof HttpError && err.response.status == 404) {
alert("No such user, please reenter.");
return demoGithubUser();
} else {
throw err;
}
});
}
demoGithubUser();