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

151
Visualizações
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 à 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