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

231
Visualizações
Function returning blank array in inspect element with inaccessible data inside

SCENARIO So I am trying to do multiple API requests with different URLs, add all the responses to an array, then return the array and use it. My code:

const getData = (dataURLs) => {
  let returnData = [];
  for (let i = 0; i < dataURLs.length; i++) {
    getFetch(dataURLs[i]).then((response) => {
      returnData.push(response);
      return response;
    });
  }
  console.log(returnData[0]);
  return returnData;
};

So this is the function that makes the requests, here is what the getFetch function is:

const getFetch = async (url) => {
  return fetch(url)
    .then((response) => {
      if (!response.ok) {
        // get error message from body or default to response status
        const error = (response && response.message) || response.status;
        return error;
      }
      return response.json();
    })
    .catch((error) => {
      return error;
    });
};

This just makes the request and returns the JSON which is what I want, and this function works as I use it in other places.

PROBLEM

My issue is, when i make the request using the getData function, it will return a blank array '[]', however when I click on this array in inspect element, it displays this.

[] ->
    0: {nodes: Array(5), edges: Array(5), self: Array(1)}
    length: 1

If I try to access anything in this array in js it just doesn't let me. But if I look at it in Inspect Element, it will be a blank array that I can expand and it displays the requested data inside it

Just wondering if anyone knew a fix to this?

Thanks :)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue you're running into here is that the function returns the array before the promises have resolved (and put the data that you want into the arrays). You will need to wait for the promises first. There are a few ways you can do this, but one way is to put the promises into the array and use a Promise.all() to get all of the values when they are available.

const getData = (dataURLs) => {
  let returnPromises = [];
  for (let i = 0; i < dataURLs.length; i++) {
    returnPromises.push(getFetch(dataURLs[i]));
  }

  return Promise.all(returnPromises);
};

From here on you will continue to use this function's result as a promise.

getData([...]).then(([result1, result2, ...resultN]) => {...})
about 4 years ago · Juan Pablo Isaza Relatório
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