Skip to content

Instantly share code, notes, and snippets.

@ethanny2
Created February 23, 2020 02:25
Show Gist options
  • Save ethanny2/9fc48802d08eba382a96e555ceb3b950 to your computer and use it in GitHub Desktop.
Save ethanny2/9fc48802d08eba382a96e555ceb3b950 to your computer and use it in GitHub Desktop.
Handle asynchronous HTTP promises synchronously with async/await. Example using Pokemon API (https://pokeapi.co/)
let urlBase = "https://pokeapi.co/api/v2/pokemon/"
const sampleGet = async (pokemonName) => {
let response = await fetch(urlBase + pokemonName);
let parsedJson = await response.json();
document.body.innerHTML = `<img alt="default front sprite for starmie" src=${parsedJson.sprites.front_default} >`;
}
sampleGet("starmie");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment