Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda