Skip to content

Commit

Permalink
[IMP] Assure it always fetch new pokemons
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelRForgeFlow committed May 17, 2023
1 parent 9ea93c1 commit d5acf27
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ function head(title: string): string {
}

(async () => {
const pokemons = await loadPokemons(20);
const pokemons = await loadPokemons(1010);
const indexHtml = renderPokemonIndex(pokemons);
await writeFile("index.html", indexHtml);

Expand Down
8 changes: 6 additions & 2 deletions pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ export class Pokemon {
}
}

export const loadPokemons = async (n: number) => {
export const loadPokemons = async (n?: number) => {
const pokemons: Array<Pokemon> = [];

for (let i = 1; i <= n; i++) {
let i = 1;
while (n ? (i <= n) : true) {

const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${i}`);
const speciesResponse = await fetch(`https://pokeapi.co/api/v2/pokemon-species/${i}`);

Expand All @@ -37,7 +39,9 @@ export class Pokemon {
pokemons.push(new Pokemon(i, data.species.name, imageUrl, types, is_baby, is_legendary, is_mythical));
} else {
console.error(`Error fetching data for Pokémon ID ${i}: ${response.statusText}`);
break;
}
i++;
}
return pokemons;
};

0 comments on commit d5acf27

Please sign in to comment.