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

240
Vistas
Async/Await fetch results into variable

I am first time using Async/Await in JS so I am confused little bit. I need to fetch JSON object from test API and store it somehow into global variable so I can use it outside async function, tried many solutions found on google but whatever I tried I am getting undefined, empty array or in most cases - Promise {}

My code currently looks like this:

let myArray = [];

async function fetchExam(id) {
  try {
      const response = await fetch('https://jsonplaceholder.typicode.com/posts', {
          method: 'GET',
          credentials: 'same-origin'
      });
      const exam = await response.json();
      return exam;
  } catch (error) {
      console.error(error);
  }
}

async function renderExam(id) {
  const exam = await fetchExam(id);
  console.log(exam);
}

renderExam()
console.log(renderExam())

I just want to assign JSON object hold in variable exam - to array called myArray. Last line returns Promise {} and console.log in renderExam function returns valid JSON. So, I need that JSON outside of async functions.

about 4 years ago · Juan Pablo Isaza
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