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

191
Vistas
Cannot call a variable inside that is defined inside .then

I am trying to do something with the posts(an array) that I am fetching from the database but as you can see I cannot call a variable that is defined inside .then. What is the right way to do this? All I want to do is fetch the array posts and do something with it.

function AllPosts() {
    fetch('/posts/all')
    .then(response => response.json())
    .then(posts => {

        ///posts printed in console here
        console.log(posts)
    })

    ///posts not printed in console here (posts are not defined error)
    console.log(posts) 
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Turn AllPosts in an async function, and await the result of the response.

async function AllPosts() {
  const response = await fetch('/posts/all');
  const posts = await response.json();

  console.log(posts) 
}
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