From d5acf278401930044945cb2fa4a25e0f8206fcee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= Date: Thu, 11 May 2023 20:55:02 +0200 Subject: [PATCH] [IMP] Assure it always fetch new pokemons --- main.ts | 2 +- pokemon.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/main.ts b/main.ts index a54a388..ad6d69c 100644 --- a/main.ts +++ b/main.ts @@ -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); diff --git a/pokemon.ts b/pokemon.ts index 784bfc3..dc7a1e8 100644 --- a/pokemon.ts +++ b/pokemon.ts @@ -17,10 +17,12 @@ export class Pokemon { } } - export const loadPokemons = async (n: number) => { + export const loadPokemons = async (n?: number) => { const pokemons: Array = []; - 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}`); @@ -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; };