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

198
Views
How to make the rest of the javascript code wait until the fetch command is finished

In my javascript code, I use the fetch() command to receive some data from my php file:

fetch('php/get_comm.php').then(response => response.text()).then(data => {
       communities = JSON.parse(data);
});

Now there is some more code to be executed that uses the data acquired using fetch.

Since fetch is an asynchronous function, the rest of the script starts executing before the fetch command finishes, hence they cannot use the variable "communities".

Currently I'm using this trick as a work around:

fetch('php/get_comm.php').then(response => response.text()).then(data => {
       console.log(JSON.parse(data));
       communities = JSON.parse(data);
       continueScript();
});

function continueScript() {
       // Rest of the script.
}

This seems like a very messy way of doing this. Is there any other more clean way to accomplish what I want?

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!