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

183
Vistas
How can I console log the promise result instead of the promise

I have routes set up for a local api. So I am trying to retrieve the api data console log just the data. But whenever I run my code it console logs the entire promise. Any ideas on how to help?

This is my code:

const onPageLoad = async () => {
    const response = await fetch(`/api/project/projects`, {
        method: 'GET',
        headers: { 'Content-Type': 'application/json' },
    })
    
    if (response.ok) {
        console.log(response.json())

    } else {
        console.error(err)
    }
}

onPageLoad();

This is what shows in the console log:

screenshot of console log

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can await the promise from response.json() to get its value:

const onPageLoad = async () => {
    const response = await fetch(`/api/project/projects`, {
        method: 'GET',
        headers: { 'Content-Type': 'application/json' },
    })
    
    if (response.ok) {
        let value = await response.json();
        console.log(value);
        // potentially do something else with value
        return value;
    } else {
        console.log('fetch() promise succeeded, but not with response.ok', response.status);
        throw new Error(`Got status ${response.status}`);
    }
}

onPageLoad().then(() => {
    console.log('all done');
}).catch(err => {
    console.log(err);
});
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