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

159
Views
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 answers
Answer question

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 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!