Skip to content

Instantly share code, notes, and snippets.

@newmedia2
Created April 12, 2020 16:23
Show Gist options
  • Save newmedia2/3bc111f73ea79cca6ea17eeffcac2cc4 to your computer and use it in GitHub Desktop.
Save newmedia2/3bc111f73ea79cca6ea17eeffcac2cc4 to your computer and use it in GitHub Desktop.
Fetch API with async/await
const getGithubUser = async user => {
const url = `https://api.github.com/users/${user}`;
try {
const response = await fetch(url);
return await response.json();
} catch(exception) {
throw `Something went wrong when fetching from ${url}: \n\n ${exception}`;
}
}
getGithubUser('newmedia2')
.then(user => console.log(user))
.catch(err => console.log(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment