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

138
Visualizações
Getting json out of a fetch promise

I found a similar question here, but when I try to get the array away from the then statement I just don't receive anything.

The below code shows in console exactly the array I want:

var url = "http://localhost:5000/products";
async function getJSON(x){ 
     let response = await fetch(x);
     let data = await response.json();
     return data;
}
getJSON(url).then(data => console.log(data)); 

0: {id: '1', name: 'xxxxx', price: 28.8, x 
parcelamento: Array(2), color: 'gray', …}
1: {id: '2', name: 'xxxxx', price: 398, 
parcelamento: Array(2), color: 'black', …}
2: {id: '3', name: 'xxxxx', price: 398.8, par...

But I want this array out of "then". I tried several ways without success:

var url = "http://localhost:5000/products";
async function getJSON(x){ 
     let response = await fetch(x);
     let data = await response.json();
     return data;
}
array = getJSON(url).then(data => data); 
console.log(array);

Instead, I receive a promise and I just can't access the array:

Promise {<pending>}
[[Prototype]]: 
Promise[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Array(14)

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