Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!