Skip to content

Commit 56854a4

Browse files
[IMP] Assure it always fetch new pokemons
1 parent 697ba25 commit 56854a4

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ function head(title: string): string {
247247
}
248248

249249
(async () => {
250-
const pokemons = await loadPokemons(386);
250+
const pokemons = await loadPokemons();
251251
const indexHtml = renderPokemonIndex(pokemons);
252252
await writeFile("index.html", indexHtml);
253253

pokemon.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ export class Pokemon {
1717
}
1818
}
1919

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

23-
for (let i = 1; i <= n; i++) {
23+
let i = 1;
24+
while (n ? (i <= n) : true) {
25+
2426
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${i}`);
2527
const speciesResponse = await fetch(`https://pokeapi.co/api/v2/pokemon-species/${i}`);
2628

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

0 commit comments

Comments
 (0)