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

230
Vistas
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 Respuestas
Responde la pregunta

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 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