Created
May 5, 2024 03:41
-
-
Save carefree-ladka/3761735301a9c611cc9dd008cd0c4820 to your computer and use it in GitHub Desktop.
List all pokemons by based on their gender
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //https://pokeapi.co/api/v2/pokemon?limit=100 | |
| //https://pokeapi.co/api/v2/gender/1 | |
| //https://pokeapi.co/api/v2/pokemon/1000 | |
| const fetcher = async (url) => { | |
| try { | |
| const res = await fetch(url) | |
| if (res.ok || res.status === 200) return await res.json() | |
| } | |
| catch (e) { | |
| console.error(e.message) | |
| } | |
| } | |
| const makePokemonList = async () => { | |
| const genders = await Promise.all([ | |
| 'https://pokeapi.co/api/v2/gender/1', | |
| 'https://pokeapi.co/api/v2/gender/2', | |
| 'https://pokeapi.co/api/v2/gender/3' | |
| ].map(async url => await fetcher(url))); | |
| const pokemonDetail = {} | |
| for (const gender of genders) { | |
| for (const detail of gender.pokemon_species_details) { | |
| const pokemonName = detail.pokemon_species.name; | |
| if (!pokemonDetail[pokemonName]) { | |
| const pokemonInfo = await fetcher(detail.pokemon_species.url); | |
| pokemonDetail[pokemonName] = { name: pokemonName, genders: [], pokemonInfo }; | |
| } | |
| pokemonDetail[pokemonName].genders.push(gender.name); | |
| } | |
| } | |
| return pokemonDetail | |
| } | |
| makePokemonList().then(res => { | |
| console.log(res); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment