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

148
Vistas
JS function returns empty array instead of data

im trying to make a function and pass a url to it and return the url data in json or array. when i call the function i get a empty array first then it will be filled. there is a small delay is guess. i cant iterate between the data and cant use the data cuz the array is empty at that moment. here is the function:

function fetch_api(url){
    const data = new Array();
    const getproducts = new XMLHttpRequest();
    getproducts.open('GET',url);
    getproducts.send();
    getproducts.onload = function(){
        if(getproducts.status == 200){
            data.push(JSON.parse(getproducts.responseText));
        }else{
            throw new Error('fetching api failed!');
        }
    }
    return data;

}

and

const data = fetch_api(all_products_api);

     console.log(data);

result: ScreenShot

about 4 years ago · Juan Pablo Isaza
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