Skip to content

Instantly share code, notes, and snippets.

@burnjohn
Created September 22, 2019 10:18
Show Gist options
  • Save burnjohn/20f8e97085e3ea5ffe82a236663c531f to your computer and use it in GitHub Desktop.
Save burnjohn/20f8e97085e3ea5ffe82a236663c531f to your computer and use it in GitHub Desktop.
const fetchPokemons = (url) => {
return fetch(url).then(response => response.json());
};
// Async example
(async () => {
const pokemon1 = await fetchPokemons('https://pokeapi.co/api/v2/pokemon/1');
console.log(pokemon1);
const pokemon2 = await fetchPokemons('https://pokeapi.co/api/v2/pokemon/10');
console.log(pokemon2);
const pokemon3 = await fetchPokemons('https://pokeapi.co/api/v2/pokemon/25');
console.log(pokemon3);
})();
// Promise.then example
fetchPokemons('https://pokeapi.co/api/v2/pokemon/1')
.then(pokemon1 => {
console.log(pokemon1);
return fetchPokemons('https://pokeapi.co/api/v2/pokemon/10');
})
.then(pokemon2 => {
console.log(pokemon2);
return fetchPokemons('https://pokeapi.co/api/v2/pokemon/25');
})
.then(pokemon3 => {
console.log(pokemon3);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment