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
How to pass a json array to a global variable

I want to pass the resulting promis array to a global variable. How can I do this? Unfortunately, I didn't understand from the documentation how to do this, but in the browser I can pass to a global variable and access the values by key. Unfortunately, the option with "foo().then(json => console.log(json))" is not suitable, because there is no initialization of the array. Thank you for any help! My code

async function foo(){
  var data = await fetch('http://127.0.0.1:8080/user'); 
  return data.json(); 
}

 let json = foo(); 
 console.log(json)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your code you assign a Promise object to you variable json

If you want to run asynchronous code, you need to pass a callback into the .then() method of Promise or wrap everything in async function and await for a result (see below).

async function foo(){
  var data = await fetch('http://127.0.0.1:8080/user'); 
  return data.json(); 
}

const global;

(async () => {
  const data = await foo();
  global = data;
  console.log('global', global)
})()



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