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

204
Vistas
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
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