Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
Fetch data from huge amount fetched URLs

Not sure how to go about this. I fetch from 1 URL to get an array of objects like this:

[{name: 'apple', url: 'appleURL'}, {name: 'orange', 'orangeURL', ...}]

then map through each URL to fetch data, the returned data for each item has another URL... like this:

[{name: 'apple', colors: ['green', 'red'], type: 'http://URL-that-needs-fetching'},{...etc}]

heres what i have, and it works but it's extremely slow...

  useEffect(() => {
    const fetchFruit = async () => {
      setLoading(true);
      const fruitResponse = await fetch(fruitPath);
      const fruitJson: Fruit =
        await fruitResponse.json();

      const fruitDetails = await Promise.all(
        fruitJson.results.map(async (f: FruitURLs) => {
          const fDetails = await fetch(f.url);
          const json: FruitDetails = await fDetails.json();

          return json;
        })
      );

      const getFruitDescriptions = await Promise.all(
        fruitDetails.map(async (item: FruitDetails, index: number) => {
          const fruitType = await fetch(item.type.url);
          const json: FruitInfo = await fruitType.json();
          return json;
        })
      );

      let full: FruitDetails[] = fruitDetails;

      for (let index = 0; index < fruitDetails.length; index++) {
        full[index].fruitInfo = getFruitDescriptions[index];
      }
      setFruitDetails(full);
      setLoading(false);
    };

    fetchFruit();
  }, []);

I'm new to using promises and not 100% confident with them, also new to TS, and react-native so apologies if there's mistakes here....

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem is that by using await() inside Promise.all(), you are resolving the fetches instead of having them happen in parallel and returning it wrapped by a promise.

You can take a reference:

const fetchFruit = async () => {
  const fruitResponse = await fetch(fruitPath);
  const fruitJson: Fruit = await fruitResponse.json();

  const fruitDetails = await Promise.all(
    fruitJson.results.map(async (f: FruitURLs) => fetch(f.url))
  );

  const getFruitDescriptions = await Promise.all(
    fruitDetails.map(async (item: FruitDetails, index: number) =>
      fetch(item.type.url)
    )
  );

  const [fruitDetailsAsJson, getFruitDescriptionsAsJson] = Promise.all([
    fruitDetails.map(async (f) => f.json()),
    getFruitDescriptions.map(async (f) => f.json()),
  ]);

  const full = fruitDetailsAsJson.map((fruit, index) => ({
    ...fruit,
    fruitInfo: getFruitDescriptionsAsJson[index],
  }));

  setFruitDetails(full);
  setLoading(false);
};

Use this resource to learn about the best practice.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda