Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] Assure it always fetch new pokemons #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};