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

161
Vistas
I want to run a JavaScript code synchronously in react native

I'm working in a project where I retrieve data from local database using SQLite then I process the data and finally I use the processed data in a calculation method.

The problem is that the code is running asynchronously so when I call the calculation method in the first time it works before the retrieve and processing methods it only works in the second or third run.

I have 4 retrieve methods :

    retrieve1();
    retrieve2();
    retrieve3();
    retrieve4();

then I want to process the retrieved data so I have the following methods:

processRetrieve1();
processRetrieve2();
processRetrieve3();
processRetrieve4();

finally I want to call the calculation method:

Calculate();

How can I run this code in the following order? Retrieve methods -> Processing methods -> Calculating method

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If those methods are working asynchronously, then you can simply wait for them to return their output by using await, and group the promises with Promise.all like this:

const retrievePromises = [retrieve1(), retrieve2(), retrieve3(), retrieve4()]
await Promise.all(retrievePromises) // Wait for all retrieve function to be resolved

const processPromises = [processRetrieve1(), processRetrieve2(), processRetrieve3(),processRetrieve4()]
await Promise.all(processPromises) // Wait for all processRetrieve function to be resolved

await Calculate() // Will then wait for the calculate function to resolve

In order to be able to use the await keyword, you have to be in the scope of an async function, like this:

const asyncFunc = async () => {
  await Promise.all(...)
}

If you want to learn more about it, I suggest you to read more the documentation, and you can read this article as well.

Note that Promise.all works asynchronously, so the promises will be resolved not necessarily in the same order as in the array. If the order matter, then you have to wait for each single function, like this:

await retrieve1();
await retrieve2();
...
about 4 years ago · Juan Pablo Isaza Denunciar
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