Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
How to handle errors for nested fetch requests in Javascript?
  • I have the following code where my fetch request gives me an array as the output.
  • Then for each element inside this array I want to fetch some data corresponding to that particular element.

NOTE : Here there are multiple requests being made after the first request gets completed. I read about Promise chaining but couldn't figure out how to implement that in this scenario where i have more than one request that depends on the promise from the first request. Please help!

useEffect(() => {
    const fetchController = new AbortController();
    const { signal } = fetchController;

    fetch(someURL, { signal })
      .then((res) => res.json())
      .then((data) => {
        // array of ids. Will make a fetch request with each of those ids.
        console.log(data.array);

        if (data.success) {
          // this is the array that i get from the outer fetch request.
          setSocialLinkList(data.array);

          // Fetch request for each of the elements in the array.
          Object.keys(data.array).forEach((index) => {

            fetch(`${functionThatGetsTheURL(data.array[index].id)}`, { signal })
              .then((response) => response.json())
              .then((responseData) => {
                console.log(responseData);
              })
              .catch((error) => {
                console.log(error);
              });
          });
        }
      }).catch((err) => {
        console.log(err);
      });

    return () => {
      // when this function is executed, i get : DOMException: The user aborted a request.
      fetchController.abort();
    };
  }, []);

The above-mentioned code works for me, however, when i abort the requests, i get the following exception in the console, despite using catch handlers to prevent the same :

Uncaught (in promise) DOMException: The user aborted a request.

Although i have added catch handlers for the requests. What is the correct way to handle this situation? How can i abort all the requests correctly?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda