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

103
Visualizações
How to return the response to console

below code is showing 'undefined' in console. while the api has data inside it.

const getFakePerson = async () => {
  let res = await fetch("https://api.randomuser.me/?nat=US&results=1");
  let { results } = res.json();
  console.log(results);
}
getFakePerson();

Result:

Result

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

0

res.json() returns a promise. You need to await it.

const getFakePerson = async () => {
  let res = await fetch("https://api.randomuser.me/?nat=US&results=1");
  let results = await res.json();
  console.log(results);
}
getFakePerson();

Alternately, you can use

res.json().then(data => console.log(data))
about 4 years ago · Juan Pablo Isaza Relatório

0

From discussion and testing the code I knew that we have to add await keyword before res.json().

const getFakePerson = async () => {
    let res = await fetch("https://api.randomuser.me/?nat=US&results=1");
    let { results } = await res.json();
    console.log(results);
}
getFakePerson();

about 4 years ago · Juan Pablo Isaza Relatório

0

The elephant in the room as pointed out already was that on line 3, there was a missing await for the res.json() call.

On top of that, I would recommend separating concerns here. Let the getRandomPerson return the person and then console.log later on in the process. Such as this:

const getFakePerson = async () => {
    let res = await fetch("https://api.randomuser.me/?nat=US&results=1");
    let { results: person } = await res.json();

    return person
}

getFakePerson().then(generatedPerson => {
   console.log(generatedPerson)
   // ... do more here ...
});

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