https bulk get request timeouts after short time while downloading images

Viewed 19

I'm trying to download some sprites from PokeAPI, and im using undici to make requests and https to download the .pngs. The code itself works fine, downloading some of the sprites i ask. The problem is that after a short while, (when i reaches ~120), it returns a timeout error:
Error: connect ETIMEDOUT at TCPConnectWrap.afterConnect [as oncomplete]. I have no idea on how to solve it.

let arr = [];
for (let i = 1; i <= 898; i++) {
  let a = await (
    await fetch(`https://pokeapi.co/api/v2/pokemon-species/${i}`)
  ).json();
  for (let j = 0; j < a.varieties.length; j++) {
    let name = a.varieties[j].pokemon.name;
    let f = await (await fetch(a.varieties[j].pokemon.url)).json();
    arr.push(name);
    https.get(f.sprites.front_default, (resp) =>
      resp.pipe(fs.createWriteStream(`sprites/${name}.png`))
    );
  }
  console.log(i);
}
0 Answers
Related