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

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

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 Denunciar

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 Denunciar

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